Skip to content

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

Merged

Conversation

Amaras
Copy link
Member

@Amaras Amaras commented Jul 3, 2020

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 the yourfile.py file, which you can run with your Python 3 installation. You can also do coconut -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 is

If you have any question, feel free to ask.

@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jul 4, 2020
Copy link
Member

@berquist berquist left a 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:

  1. You accidentally put the code in a folder called cocount instead of coconut.

  2. 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)
Copy link
Member

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[import:13-16, lang="coconut"](code/coconut/euclidean.coco)
[import:12-15, lang="coconut"](code/coconut/euclidean.coco)

@Amaras
Copy link
Member Author

Amaras commented Jul 5, 2020

Thanks for the review.
It's mainly Python, so 4 spaces, but I forgot to check that it was indenting using spaces.
I'll check with my current and future PRs for Coconut code.

@Amaras Amaras requested a review from berquist July 5, 2020 22:14
@berquist
Copy link
Member

berquist commented Jul 5, 2020

Can you make the last Markdown change?

@Amaras
Copy link
Member Author

Amaras commented Jul 5, 2020

Yep, I forgot to take that into account. Probably done by now.

@berquist
Copy link
Member

berquist commented Jul 5, 2020

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 COCO, which you can override with the Name field.

@Amaras
Copy link
Member Author

Amaras commented Jul 5, 2020

Yeah, I didn't know how to do that. Should be good now, if I did it right.

Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right. Thanks!

@berquist berquist merged commit 6d02f4e into algorithm-archivists:master Jul 5, 2020
@Amaras Amaras deleted the euclidean_algorithm_in_coconut branch July 5, 2020 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants