views:

37

answers:

1

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.

+1  A: 

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.

sepp2k
The definition you have sent has the type; `val foo = fn : 'a -> ('a * int -> 'b) -> 'b` . My query is how can a function which does not take a list, return a list, which does not happen in the definition you have given above
darkie15
@darkie15: Have you seen the edit? The new version matches your type exactly.
sepp2k
Okie just saw the edit. It's working as per the type expression I needed. Can you please let me know how to derive such ML expression based on the type expression given? I want to be able to solve any such derivations given .
darkie15