开发者

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 in开发者_JAVA技巧t 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜