有很多基于 的问题applyTwice,但没有一个与我的问题有关。我了解applyTwice函数定义如下:
applyTwice :: (a -> a) -> a -> a
applyTwice f a = f (f a)
Run Code Online (Sandbox Code Playgroud)
两次应用一个函数。所以如果我有一个增量函数:
increment x = x + 1
Run Code Online (Sandbox Code Playgroud)
并做
applyTwice increment 0
Run Code Online (Sandbox Code Playgroud)
我得到 2. 但我不明白这些结果:
applyTwice applyTwice applyTwice increment 0 -- gives 16
applyTwice applyTwice applyTwice applyTwice increment 0 -- gives 65536
applyTwice applyTwice applyTwice applyTwice applyTwice increment 0 -- stack overflow
Run Code Online (Sandbox Code Playgroud)
我也知道
twice = applyTwice applyTwice increment
applyTwice twice 0 -- gives 8
Run Code Online (Sandbox Code Playgroud)
我根本无法理解这些结果,如果有人能解释一下,我会很高兴的。如果这是基本的东西,我很抱歉,因为我只是在学习 Haskell。
haskell functional-programming currying higher-order-functions