commit 421a84350238990006031c627652518ea71f137d parent cfa32faf70488b57623324ec3b07c2292ee5c295 Author: Jared Tobin <jared@jtobin.ca> Date: Thu, 5 Mar 2015 23:05:24 +1300 Add power exercise. Diffstat:
A | 20150303_power/Power.hs | | | 23 | +++++++++++++++++++++++ |
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/20150303_power/Power.hs b/20150303_power/Power.hs @@ -0,0 +1,23 @@ + +module Power where + +import Data.Foldable +import Data.Monoid + +powLinear :: Num c => Product c -> Int -> c +powLinear a b = getProduct . fold $ replicate b a + +powLogarithmic :: (Num b, Integral a) => b -> a -> b +powLogarithmic a b + | b < 0 = error "nonnegative powers only" + | b == 0 = 1 + | even b = + let term = powLogarithmic a (b `quot` 2) + in term * term + | otherwise = + let term = powLogarithmic a (pred b `quot` 2) + in a * term * term + +powConstant :: Floating a => a -> a -> a +powConstant a b = exp (log a * b) +