Skip to content

Modified euclidian algorithm haskell code to be more clear and readable #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Unlambder
Kjetil Johannessen
CDsigma
DominikRafacz
lulucca12
28 changes: 12 additions & 16 deletions chapters/euclidean_algorithm/code/haskell/euclidean_example.hs
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions chapters/euclidean_algorithm/euclidean.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -136,4 +136,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$