Given:
fun twice (f: int->int) = fn(x: int):int => f (f (x)) val fourth = twice (fn (x:int) => x * x) val quad = twice (fn (x:int) => 2 * x)
Trying to evaluate fourth (3) does result in 81:
fourth(3) --> (twice (fn (x:int) => x * x)) (3) --> (fn(x: int):int => (fn (x:int) => x * x) ((fn (x:int) => x * x) (x))) (3) --> (fn (x:int) => x * x) ((fn (x:int) => x * x) (3)) --> (fn (x:int) => x * x) (3*3) --> (fn (x:int) => x * x) (9) --> 9*9 --> 81
Note that when we apply an anonymous function fn
p
=>
e, the body of the function is the
expression e.