diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 807513539..0323542f6 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -14,3 +14,4 @@ Unlambder Kjetil Johannessen CDsigma DominikRafacz +lulucca12 diff --git a/chapters/euclidean_algorithm/code/haskell/euclidean_example.hs b/chapters/euclidean_algorithm/code/haskell/euclidean_example.hs index e292e3457..e09b450c9 100644 --- a/chapters/euclidean_algorithm/code/haskell/euclidean_example.hs +++ b/chapters/euclidean_algorithm/code/haskell/euclidean_example.hs @@ -1,25 +1,21 @@ -- contributed by Nicole Mazzuca (ubsan) - euclidSub :: Integer -> Integer -> Integer -euclidSub a b = inner (abs a) (abs b) where - inner a b = - if a == b then - a - else if a < b then - euclidSub a (b - a) - else - euclidSub (a - b) b +euclidSub a b = inner (abs a) (abs b) + where + inner x y + | x == y = x + | x < y = euclidSub x (y - x) + | otherwise = euclidSub (x - y) y euclidMod :: Integer -> Integer -> Integer -euclidMod a b = inner (abs a) (abs b) where - inner a 0 = a - inner a b = inner b (a `mod` b) +euclidMod a b = inner (abs a) (abs b) + where + inner x 0 = x + inner x y = inner y (x `mod` y) main :: IO () main = do let chk1 = euclidMod (64 * 67) (64 * 81) chk2 = euclidSub (128 * 12) (128 * 77) - putStrLn (show chk1) - putStrLn (show chk2) - return () - + print chk1 + print chk2 diff --git a/chapters/euclidean_algorithm/euclidean.md b/chapters/euclidean_algorithm/euclidean.md index 609271b47..cdf01402a 100644 --- a/chapters/euclidean_algorithm/euclidean.md +++ b/chapters/euclidean_algorithm/euclidean.md @@ -20,7 +20,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="py" %} [import:11-22, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} -[import:3-11, lang="haskell"](code/haskell/euclidean_example.hs) +[import:2-8, lang="haskell"](code/haskell/euclidean_example.hs) {% sample lang="rs" %} [import:3-15, lang="rust"](code/rust/euclidean_example.rs) {% sample lang="ml" %} @@ -53,7 +53,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="py" %} [import:1-9, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} -[import:13-24, lang="haskell"](code/haskell/euclidean_example.hs) +[import:10-14, lang="haskell"](code/haskell/euclidean_example.hs) {% sample lang="rs" %} [import:17-27, lang="rust"](code/rust/euclidean_example.rs) {% sample lang="ml" %} @@ -136,4 +136,3 @@ $$ \newcommand{\bfomega}{\boldsymbol{\omega}} \newcommand{\bftau}{\boldsymbol{\tau}} $$ -