Take the 2-minute tour ×

Here is an example I picked up from this ebook (http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf)

-fun curry (f:'a * 'b -> 'c) = fn (x:'a) => fn (y:'b) => f (x,y);
val curry = fn : ('a * 'b -> 'c) -> 'a -> 'b -> 'c

How do I interpret this function. Curry takes as argument a function f of type 'a * 'b -> 'c. I can't understand the part after '='. What is the associativity order?

Here is another example:

fun add’ (x:int) (y:int):int = x + y;

How is this parsed?

Wikipedia says "currying is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument (partial application)". Which is the single argument: the first or the last one out of the multiple arguments?

share|improve this question
I had the same question in haskell. So this may help you stackoverflow.com/questions/3794371/… –  Matt Jul 30 '11 at 6:05
@Matt: I got more confused after reading it :) Thanks for the link though. –  Bruce Jul 30 '11 at 6:08
add comment

1 Answer

up vote 2 down vote accepted

fn (x:'a) => fn (y:'b) => f (x,y) is parsed as fn (x:'a) => (fn (y:'b) => f (x,y)). So you have a function, which takes an argument x of type a and returns another function, which takes an argument y of type b. This other function then returns the result of calling f (x,y).

fun foo x y = ... is syntactic sugar for val foo = fn x => fn y => ..., so again foo is a function, which takes one argument x and returns another function, which takes one argument y.

Similarly the call foo 1 2 will be parsed as (foo 1) 2, i.e. it calls the function foo with the argument 1 and then calls the resulting function with the argument 2.

share|improve this answer
@ sepp2k: I have just started out with SML. Can you please suggest a good resource (book, online material) which I can use. –  Bruce Jul 30 '11 at 16:07
add comment

Your Answer


