import Data.List myPowerLinear, myPowerLog, myPowerTail, myPowerTailStrict :: Int -> Int -> Int myPowerFoldr, myPowerFoldl, myPowerFoldl' :: Int -> Int -> Int -- really dumb recursive version myPowerLinear _ 0 = 1 myPowerLinear x y = x * myPowerLinear x (y-1) -- slightly clever recursive version myPowerLog x y | y==0 = 1 | even y = half * half | odd y = half * half * x where half = myPowerLog x (div y 2) -- tail-recursive version myPowerTail x y = powRec 1 x y where -- powRec m x0 y0 == m*(x0^y0) powRec m _ 0 = m powRec m x0 y0 = powRec (m*x0) x0 (y0-1) -- tail-recursive, with strict evaluation of the accumulator myPowerTailStrict x y = powRec 1 x y where -- powRec m x0 y0 == m*(x0^y0) powRec m _ 0 = m powRec m x0 y0 = (powRec $! (m*x0)) x0 (y0-1) -- foldr/foldl equivalents myPowerFoldr x y = foldr (*) 1 xs where xs = take y (repeat x) myPowerFoldl x y = foldl (*) 1 xs where xs = take y (repeat x) myPowerFoldl' x y = foldl' (*) 1 xs where xs = take y (repeat x) main :: IO () main = print (myPowerLinear 1 1000000) {- Right-sized example for Greg's netbook: myPowerX 1 1000000 -}