Skip to content

Minor changes to euclidean algorithm implementation in Common Lisp #609

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

Trashtalk217
Copy link
Contributor

The euclidean algorithm was the first algorithm I ever implemented in Common Lisp and it is due for a revision. I added docstrings and did some changes to imdentation. I also did some research into turning the two functions for euclid-sub and turning it into one single function. I decided against it since alternative implementations were slower.

That's about it, some very small changes to make the code slightly better.

Copy link
Contributor

@dovisutu dovisutu left a comment

Choose a reason for hiding this comment

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

I don't know much about Common Lisp, but I find 2 small mistakes in .md file.

@c252 c252 added the Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) label Jun 15, 2019
@Trashtalk217
Copy link
Contributor Author

Trashtalk217 commented Jul 17, 2019

There is a weird thing, where the indentation in the euclid-sub* doesn't work. (euclid-sub* (- a b) b) and (euclid-sub* a (- b a)) line up with (if instead of (> a b). It looks fine in my editor, but when serverd it's wrong.

Could someone try to build it on their own machine and check how it looks?

EDIT: I think the .md problems from @dovisutu are fixed.

@Trashtalk217
Copy link
Contributor Author

Trashtalk217 commented Jul 17, 2019

Fixed it. This is PR is good to go in my eyes. A review is still needed and appreciated though.

@berquist
Copy link
Member

berquist commented Sep 1, 2019

The rename confused Git, so here is the actual diff:

diff --git a/euclidean_algorithm.lisp b/temp.lisp
index cff5ab2..3b8dcad 100644
--- a/euclidean_algorithm.lisp
+++ b/temp.lisp
@@ -1,33 +1,32 @@
-;;;; Euclid algorithm implementation
+;;;; Euclidean algorithm implementation in Common Lisp

 (defun euclid-sub (a b)
+  "Finds the greatest common divsor for any two integers"
   (euclid-sub* (abs a) (abs b)))

 (defun euclid-sub* (a b)
+  "Finds the greatest common divisor for any two positive integers"
   (if (eq a b)
-    a
-    (if (> a b)
-      (euclid-sub* (- a b) b)
-      (euclid-sub* a (- b a)))))
+      a
+      (if (> a b)
+             (euclid-sub* (- a b) b)
+             (euclid-sub* a (- b a)))))

 (defun euclid-mod (a b)
+  "Finds the greatest common divisor for any two integers"
   (if (zerop b)
-    (abs a)
-    (euclid-mod b (mod a b))))
+      (abs a)
+      (euclid-mod b (mod a b))))

-(print
-  (euclid-sub (* 64 67)
-              (* 64 81)))
-(print
-  (euclid-mod (* 128 12)
-              (* 128 77)))
+(print (euclid-sub (* 64 67) (* 64 81)))
+(print (euclid-mod (* 128 12) (* 128 77)))

-;; built-in function: (gcd 80 40)
-;; quick test
-(assert
-  (=  (euclid-sub (* 64 67) (* 64 81))
-      (gcd (* 64 67) (* 64 81))))
+;; Quick test
+(assert
+ (eql  (euclid-sub (* 64 67) (* 64 81))
+       (gcd (* 64 67) (* 64 81))))

-(assert
-  (=  (euclid-mod (* 64 67) (* 64 81))
+(assert
+ (eql (euclid-mod (* 64 67) (* 64 81))
       (gcd (* 64 67) (* 64 81))))
+

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.

Everything still functionally works, so that's good. I also approve of the change from = to eql according to https://stackoverflow.com/a/548774. Just a few nits left.

(I have noticed that out of the box, Emacs does not indent CL like this, so I'll provide some instructions on the wiki for those of us who use it on changing that.)

@Trashtalk217 Trashtalk217 force-pushed the euclidean-changes-lisp branch from 5ac602a to 329f333 Compare September 8, 2019 17:58
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 good. The breakage is due to some transitive dependencies having nothing to do with your code (see #620 (comment)). I'll wait to merge until we know more about that.

@berquist berquist self-assigned this Sep 9, 2019
@berquist berquist merged commit afcaf4d into algorithm-archivists:master Sep 9, 2019
@berquist
Copy link
Member

berquist commented Sep 9, 2019

@Trashtalk217 Something I commented on indirectly: the file rename made it hard to compare changes (see #609 (comment)). If you're going to do a file rename, in order for Git to not get confused, it's best to either

  1. In one or more commits, make all your changes, and then finally make one commit where you rename the file, or
  2. rename the file before making any changes, commit, and then make your change commits.

This way you can force git diff to actually show the changes, rather than the temp file hack I needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants