可以使用lambda表达式定义匿名函数:
-- Contrived example
> apply f x = f x
> apply (\n -> n + 1) 3
4
但是可以定义一个匿名的递归函数吗?
> apply (\n -> n * ??? (n - 1)) 3 -- compute a factorial
6
答案 0 :(得分:3)
导入Data.Function
并使用fix
函数计算非 - 递归函数的固定点,该函数将函数递归地应用为参数
> import Data.Function
> :t fix
fix :: (a -> a) -> a
> apply (fix $ \f n -> if n == 0 then 1 else n * f (n - 1)) 3
6