Exercise 9.7

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”

  1. J says:

    More concisely:

    twice f = f . f

Leave a Reply