From d9f74581e69262e43f859cc04df8b9df261bd2cd Mon Sep 17 00:00:00 2001 From: c252 Date: Sat, 26 Jan 2019 16:26:51 -0500 Subject: [PATCH 01/11] euclid alg in scheme --- book.json | 4 +++ .../code/scheme/euclidalg.ss | 25 +++++++++++++++++++ .../euclidean_algorithm.md | 6 +++++ 3 files changed, 35 insertions(+) create mode 100644 contents/euclidean_algorithm/code/scheme/euclidalg.ss diff --git a/book.json b/book.json index f15df90ae..839600e50 100644 --- a/book.json +++ b/book.json @@ -188,6 +188,10 @@ { "lang": "piet", "name": "Piet" + }, + { + "lang": "ss", + "name": "Scheme" } ] } diff --git a/contents/euclidean_algorithm/code/scheme/euclidalg.ss b/contents/euclidean_algorithm/code/scheme/euclidalg.ss new file mode 100644 index 000000000..7e0da36d4 --- /dev/null +++ b/contents/euclidean_algorithm/code/scheme/euclidalg.ss @@ -0,0 +1,25 @@ +(define euclid-sub + (lambda (a b) + (cond + ((or (negative? a) (negative? b)) + (euclid-sub (abs a) (abs b))) + (else (cond + ((eq? a b) a) + (else (cond + ((> a b) + (euclid-sub (- a b) b)) + (else + (euclid-sub a (- b a)))))))))) + +(define euclid-mod + (lambda (a b) + (cond + ((or (negative? a) (negative? b)) + (euclid-mod (abs a) (abs b))) + (else (cond + ((zero? b) a) + (else + (euclid-mod b (modulo a b)))))))) + +(display (euclid-mod (* 64 67) (* 64 81))) (newline) +(display (euclid-sub (* 64 12) (* 64 27))) (newline) \ No newline at end of file diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 84aa60f3f..adee422cb 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -65,6 +65,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:24-38, lang="bash"](code/bash/euclid.bash) {% sample lang="piet" %} > ![](code/piet/subtract/euclidian_algorithm_subtract_large.png) ![](code/piet/subtract/euclidian_algorithm_subtract.png) +{% sample lang="ss" %} +[import:1-12, lang="scheme"](code/scheme/euclidalg.ss) {% endmethod %} Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this: @@ -136,6 +138,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:10-22, lang="bash"](code/bash/euclid.bash) {% sample lang="piet" %} > ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png) +{% sample lang="ss" %} +[import:14-22, lang="scheme"](code/scheme/euclidalg.ss) {% endmethod %} Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps: @@ -232,6 +236,8 @@ A text version of the program is provided for both versions. > ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png) [import:126-146](code/piet/euclidian_algorithm.piet) +{% sample lang="ss" %} +[import:, lang="scheme"](code/scheme/euclidalg.ss) {% endmethod %}