From 3200035c3e14e2196b7c88652bfafd28b6220860 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 30 Nov 2018 22:07:13 +0100 Subject: [PATCH 1/8] added language Prolog --- .editorconfig | 4 ++++ book.json | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 0faa1c175..a9ac4162b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -152,3 +152,7 @@ indent_size = 0 trim_trailing_whitespace = false insert_final_newline = false end_of_line = lf + +[*.pl] +indent_style = space +indent_size = 0 diff --git a/book.json b/book.json index a6326ffef..a8049f6a4 100644 --- a/book.json +++ b/book.json @@ -167,7 +167,11 @@ { "lang": "lolcode", "name": "LOLCODE" - } + }, + { + "lang": "pl", + "name": "Prolog" + } ] } } From 48258ee4e5eb8c6a80833c66115bd1ec67d687f7 Mon Sep 17 00:00:00 2001 From: PaddyKe <34421580+PaddyKe@users.noreply.github.com> Date: Fri, 30 Nov 2018 22:07:47 +0100 Subject: [PATCH 2/8] add euclidean algorithm in Prolog --- .../code/prolog/euclidean_algorithm.pl | 12 ++++++++++++ contents/euclidean_algorithm/euclidean_algorithm.md | 6 ++++++ 2 files changed, 18 insertions(+) create mode 100644 contents/euclidean_algorithm/code/prolog/euclidean_algorithm.pl diff --git a/contents/euclidean_algorithm/code/prolog/euclidean_algorithm.pl b/contents/euclidean_algorithm/code/prolog/euclidean_algorithm.pl new file mode 100644 index 000000000..28874d972 --- /dev/null +++ b/contents/euclidean_algorithm/code/prolog/euclidean_algorithm.pl @@ -0,0 +1,12 @@ +euclid_sub(A, A, A) :- !. +euclid_sub(A, B, Result) :- A1 is abs(A), B1 is abs(B), + A1 > B1, !, T is A1 - B1, euclid_sub(T, B, Result). +euclid_sub(A, B, Result) :- A1 is abs(A), B1 is abs(B), + A1 =< B1, !, T is B1 - A1, euclid_sub(A, T, Result). + +euclid_mod(A, 0, A) :- !. +euclid_mod(A, B, Result) :- A1 is abs(A), B1 is abs(B), + M is mod(A1, B1), euclid_mod(B, M, Result). + +test_euclid_sub(Result) :- euclid_sub(4288, 5184, Result). % Result: 64 +test_euclid_mod(Result) :- euclid_mod(1536, 9856, Result). % Result: 128 diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 572ba6129..50cb46ffa 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -63,6 +63,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol) {% sample lang="bash" %} [import:24-38, lang="bash"](code/bash/euclid.bash) +{% sample lang="pl" %} +[import:1-5, lang="Prolog"](code/prolog/euclidean_algorithm.pl) {% 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: @@ -132,6 +134,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:9-23, lang="LOLCODE"](code/lolcode/euclid.lol) {% sample lang="bash" %} [import:10-22, lang="bash"](code/bash/euclid.bash) +{% sample lang="pl" %} +[import:7-10, lang="Prolog"](code/prolog/euclidean_algorithm.pl) {% 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: @@ -209,6 +213,8 @@ and modulo method: [import, lang="LOLCODE"](code/lolcode/euclid.lol) {% sample lang="bash" %} [import, lang="bash"](code/bash/euclid.bash) +{% sample lang="pl" %} +[import, lang="Prolog"](code/prolog/euclidean_algorithm.pl) {% endmethod %}