Anonymous Function.

匿名函数是没有名字的函数,因此其可以方便的作为其他函数的参数或者返回值。但这也使得它无法进行递归。原因在于:evaluate function bindings 时需要将函数的名字 x0 加入环境以实现后续的递归。

1
(fn x => e)

我们可以把关键字 fun 视为 syntactic sugar:下面这两个表达其实是一致的。

1
2
fun x = e
val x = fn x => e

在使用匿名函数时,要注意避免 unnecessary function wrapping:

1
2
fun nth_tail_poor (n, x) = n_times((fn y => tl y), n, x)
fun nth_tail_neat (n, x) = n_times(tl, n, x)