segfault
segfault

Reputation: 5939

Standard ML: how to execute another function within a function?

Currently, my code looks like this:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0);
        val void = mapi gradient iImg;
    in
        Gdimage.toPng iImg "gradient.png"
    end;

mapi is a function with type intint->intint*int->image->unit. Essentially it operates on the image supplied.

The function looks ugly with val void = ...

How could I eliminate that?

Upvotes: 1

Views: 1665

Answers (3)

Norman Ramsey
Norman Ramsey

Reputation: 202495

I actually prefer the additional type-checking offered by

val () = mapi gradient iImg

If this looks ugly, it should—it's not very functional, and Standard ML is definitely a functional language.

Upvotes: 1

JaakkoK
JaakkoK

Reputation: 8387

You can have a list of expressions between in and end in a let expression. So you could rewrite your code to

fun gradImage () =
    let
        val iImg = Gdimage.image(640,480) (0,0,0)
    in
        mapi gradient iImg;
        Gdimage.toPng iImg "gradient.png"
    end;

I assume mapi modifies iImg in place, as the code seems to be written that way. That doesn't sound very functional; it would feel more natural to have mapi return the modified image, but from what I can see of the Gdimage interface, it looks like how it's done there, and I understand it's probably better from efficiency perspective.

Upvotes: 2

Vadim K.
Vadim K.

Reputation: 2436

It's been a decade since I've used SML, but I believe what you're looking for is this:

fun gradImage () =
    let val iImg = Gdimage.image(640,480) (0,0,0)
    in
        ignore (mapi gradient iImg);
        Gdimage.toPng iImg "gradient.png"
    end;

Upvotes: 1

Related Questions