diff --git a/src/Maths/Abs.hs b/src/Maths/Abs.hs new file mode 100644 index 0000000..ef349b4 --- /dev/null +++ b/src/Maths/Abs.hs @@ -0,0 +1,9 @@ +module Maths.Abs where + +import Prelude hiding (abs) + +-- The absolute value of a number greater than or equal to 0 is simply that number. +-- The absolute value of a number less than 0 is the negation of that number +abs :: (Num a, Ord a) => a -> a +abs n | n < 0 = negate n + | otherwise = n diff --git a/src/Maths/PowerUsingRecursion.hs b/src/Maths/PowerUsingRecursion.hs new file mode 100644 index 0000000..a7025f8 --- /dev/null +++ b/src/Maths/PowerUsingRecursion.hs @@ -0,0 +1,8 @@ +module Maths.PowerUsingRecursion where + +-- Raise a base to the power of the exponent using recursion + +power :: Int -> Int -> Int +power _ 0 = 1 +power b n | n > 0 = b * power b (n -1) + | otherwise = error "Integer exponentiation not defined for negative exponents"