twice :: (a -> a) -> a -> a
twice f = (\x -> f (f x)) |

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 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.

This entry was posted on Wednesday, August 8th, 2007 at 11:13 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

More concisely:

twice f = f . f