高阶函数
在Felix中,函数是第一类,这意味着函数可以用作值。接受另一个函数作为参数,或作为结果返回函数的函数称为 higher order function ,简称 HOF .
下面是一个例子:
fun twice (x:int):int => x + x;
fun thrice (x:int):int => x + x + x;
fun paired (f:int->int, a:int, b:int) =>
f a, f b
;
println$ paired (twice,2,3);
println$ paired (thrice,2,3);
这里,函数 twice 作为参数传递给 paired ,绑定到参数 f ,然后应用于参数 a 和 b 以获得最终结果。
然后,我们再做一次,这一次过去了 thrice 相反。