-
-
Notifications
You must be signed in to change notification settings - Fork 359
Euclidean algorithm in coconut #721
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
Euclidean algorithm in coconut #721
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool. Two other things:
-
You accidentally put the code in a folder called
cocount
instead ofcoconut
. -
We use EditorConfig to try and get consistent code formatting (tabs vs. spaces and number of spaces for indentation, among other things). Can you modify
.editorconfig
at the top level to have
# Coconut
[*.coco]
indent_style = tab
or use spaces with some fixed width (I'm not sure what it's supposed to be). I also recommend installing a plugin for your text editors/IDEs of choice so you can take advantage of this.
elif a < b: | ||
return euclid_sub(a, b - a) | ||
elif b < a: | ||
return euclid_sub(a - b, b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you order this like the Python implementation, where there's implicit fallthrough for the a == b
case?
@@ -81,6 +81,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two | |||
|
|||
{% sample lang="ps1" %} | |||
[import:1-14, lang="powershell"](code/powershell/euclidean_algorithm.ps1) | |||
{% sample lang="coco" %} | |||
[import:1-11, lang="coconut"](code/coconut/euclidean.coco) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[import:1-11, lang="coconut"](code/coconut/euclidean.coco) | |
[import:1-10, lang="coconut"](code/coconut/euclidean.coco) |
(assuming you don't do the case reordering)
@@ -169,6 +171,8 @@ Modern implementations, though, often use the modulus operator (%) like so | |||
|
|||
{% sample lang="ps1" %} | |||
[import:16-27, lang="powershell"](code/powershell/euclidean_algorithm.ps1) | |||
{% sample lang="coco" %} | |||
[import:13-21, lang="coconut"](code/coconut/euclidean.coco) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[import:13-21, lang="coconut"](code/coconut/euclidean.coco) | |
[import:13-16, lang="coconut"](code/coconut/euclidean.coco) |
@@ -169,6 +171,8 @@ Modern implementations, though, often use the modulus operator (%) like so | |||
|
|||
{% sample lang="ps1" %} | |||
[import:16-27, lang="powershell"](code/powershell/euclidean_algorithm.ps1) | |||
{% sample lang="coco" %} | |||
[import:13-16, lang="coconut"](code/coconut/euclidean.coco) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[import:13-16, lang="coconut"](code/coconut/euclidean.coco) | |
[import:12-15, lang="coconut"](code/coconut/euclidean.coco) |
Thanks for the review. |
Can you make the last Markdown change? |
Yep, I forgot to take that into account. Probably done by now. |
There is something else I missed. You should modify https://github.com/algorithm-archivists/algorithm-archive/blob/master/book.json to have an entry for Coconut in the language picker. Right now, it shows as |
Yeah, I didn't know how to do that. Should be good now, if I did it right. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks right. Thanks!
Here is the euclidean algorithm as one could implement it in the Coconunt programming language.
It uses pattern matching, and Coconut optimizes tail call recursion, so the algorithm will not exceed max recursion depth.
As it's the first time someone seems to have implemented an AAA algorithm in this language, there are a few things you need to know about it.
First (some advertising for Coconut): Coconut is compiled language which is a strict superset of Python 3: all valid Python code is valid Coconut code. The Coconut compiler compiles to Python. Coconut also adds more functional programming features built-in than Python does. You can find their website at: http://coconut-lang.org, and docs at https://coconut.readthedocs.io/en/master/DOCS.html
Second: Installation instructions:
pip install coconut
that's all.Third (and finally): Compilation instructions:
coconut yourfile.coco
compiles to theyourfile.py
file, which you can run with your Python 3 installation. You can also dococonut -r yourfile
, which runs the Python file after compilation (it also keeps the file on your system). I can add the file if needed, but it isIf you have any question, feel free to ask.