既然您了解一切都是单参数函数,那么让我们从这一点开始。请记住,(\xyz -> x) 实际上是 (\x -> (\yz -> x)),而这又是 (\x -> (\y -> (\z -> x)) ),但让我们在第一步停止,以降低括号中的噪音。
(f . g) x = f (g x)
因此
((\x -> (\y z -> x)) . (\a b -> a*b)) 2 =
(\x -> (\y z -> x)) ((\a -> (\b -> a*b)) 2) =
(\x -> (\y z -> x)) (\b -> 2*b) =
(\y z -> (\b -> 2*b))
现在记住第二步并展开 (\yz -> ...):
(\y z -> (\b -> 2*b)) 3 4 =
(\y -> (\z -> (\b -> 2*b))) 3 4 =
-- \y -> ... given anything, returns a function \z -> ...
(\z -> (\b -> 2*b)) 4 =
-- \z -> ... given anything, returns a function \b -> ...
(\b -> 2*b)
最后是:
(\b -> 2*b) 5 = 2*5 = 10
如果第一个函数返回 y 而不是 x,故事会以不同的方式展开:
((\x -> (\y z -> y)) . (\a -> (\b -> a*b))) 2 =
(\x -> (\y z -> y)) ((\a -> (\b -> a*b)) 2) =
(\x -> (\y z -> y)) (\b -> 2*b) =
-- \x -> ... given anything, returns a function \y z -> ...
(\y z -> y)
所以你得到:
(\y -> (\z -> y)) 3 4 5 =
-- \y -> ... given anything, returns a function \z -> ...
(\z -> 3) 4 5 =
-- \z -> ... given anything, returns a constant 3
3 5 -- now still trying to apply 5 to 3
它试图3
将5
.