What does "=>" mean in standard ml?

Viewed 175

Seems to mean a function outputs this... but I can't find it explained anywhere. I know -> is for types, I'm asking for => to be explained.

val twice = fn f => fn x => f(f x);

^Example

1 Answers

Together with the keyword fn they form lambda expressions, i.e. anonymous functions. In your example, twice gets assigned such a lambda expression. With more obvious parantheses:

val twice = (fn f => (fn x => f(f x)))

twice is therefore a function that takes a function, and returns another function (another lambda expression). When you call a lambda expression, the same things happen like when you call a normal function:

- fun plus2 x = x + 2;
> val plus2 = fn : int -> int
- twice plus2 4
> val it = 8 : int

twice plus2 means we call twice with plus2 as the parameter f, therefore it is equivalent to fn x => plus2(plus2 x). When we then call the result on 4, x is replaced by 4, leading to plus2(plus2 4) which results in 8.


Function definition using fun can be seen as syntactic sugar for assigning lambda expressions to names, i.e. the following two lines are equivalent:

fun foo x = x * 3
val foo = fn x => x * 3
Related