power :: (a -> a) -> Int -> a -> a
power f 0 = (\x -> x)
power f n = (\x -> f (power f (n-1) x)) |

power :: (a -> a) -> Int -> a -> a
power f 0 = (\x -> x)
power f n = (\x -> f (power f (n-1) x))

Since the function is called `power`, there is one obvious application that springs to mind, i.e. raising to a power:

raise :: Int -> Int -> Int
raise x y = power (*x) y 1 |

raise :: Int -> Int -> Int
raise x y = power (*x) y 1

This entry was posted on Wednesday, August 8th, 2007 at 11:18 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.

The following is a possible definition for power using

higher-order functions:

It can be simplified to:

power f n = foldr (.) id (replicate n f)