twice :: (a -> a) -> a -> a
twice f = (\x -> f (f x))
twice twice applies the function 4 times:
twice twice f
= twice (\y -> f (f y))
= (\x -> (\y -> f (f y)) ((\y -> f (f y)) x))
= (\x -> (\y -> f (f y)) (f (f x)))
= (\x -> f (f (f (f x))))
twice twice twice and twice (twice twice) each apply the function 16 times.
One response to “Exercise 9.7”
More concisely:
twice f = f . f