name_masked
name_masked

Reputation: 9794

Query on type expressions in ML

All,

Here is the type expression which I need to convert to a ML expression:

int -> (int*int -> 'a list) -> 'a list

Now I know this is a currying style expression which takes 2 arguments: 1st argument = Type int and 2nd argument = Function which takes the previous int value twice and return a list of any type

I am having a hard time figuring such a function that would take an int and return 'a list.

I am new to ML and hence this might be trivial to others, but obviously not me.

Any help is greatly appreciated.

Upvotes: 1

Views: 134

Answers (1)

sepp2k
sepp2k

Reputation: 370102

You get an int and a function int*int -> 'a list. You're supposed to return an 'a list. So all you need to do is call the function you get with (x,x) (where x is the int you get) and return the result of that. So

fun foo x f = f (x,x)

Note that this is not the only possible function with type int -> (int*int -> 'a list) -> 'a list. For example the functions fun foo x f = f (x, 42) and fun foo x f = f (23, x) would also have that type.

Edit:

To make the type match exactly add a type annotation to restrict the return type of f:

fun foo x (f : int*int -> 'a list) = f (x,x)

Note however that there is no real reason to do that. This version behaves exactly as the one before, except that it only accepts functions that return a list.

Upvotes: 1

Related Questions