From 0140f8fdbacada9a7a9a6c6a3f023a4b8e84a5e4 Mon Sep 17 00:00:00 2001 From: tredontho Date: Sun, 3 Oct 2021 14:43:18 -0500 Subject: [PATCH 1/2] Add absolute value --- src/Maths/Abs.hs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/Maths/Abs.hs 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 From 3646c5ef51b86ec1ce2dc4a771f36c2ab72a8327 Mon Sep 17 00:00:00 2001 From: tredontho Date: Sun, 3 Oct 2021 14:58:51 -0500 Subject: [PATCH 2/2] Add recursive exponentiation algorithm --- src/Maths/PowerUsingRecursion.hs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Maths/PowerUsingRecursion.hs 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"