2

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?

Bruce
  • 33,927
  • 76
  • 174
  • 262
  • I had the same question in haskell. So this may help you http://stackoverflow.com/questions/3794371/haskell-currying-need-further-explanation – Matt Jul 30 '11 at 06:05
  • @Matt: I got more confused after reading it :) Thanks for the link though. – Bruce Jul 30 '11 at 06:08

1 Answers1

2

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • @ 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