Last active
September 27, 2015 23:21
-
-
Save verdammelt/6f2ba3ea7cdeeea9b38e to your computer and use it in GitHub Desktop.
raindrops implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun convert (n &optional (cases '((3 . "Pling") (5 . "Plang") (7 . "Plong")))) | |
(let ((results | |
(do* ((cs cases (cdr cs)) | |
(c (car cs) (car cs)) | |
(result (list))) | |
((null cs) (reverse result)) | |
(when (zerop (mod n (car c))) | |
(push (cdr c) result))))) | |
(if results | |
(apply #'concatenate 'string results) | |
(write-to-string n)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun convert (n &optional (cases '((3 . "Pling") (5 . "Plang") (7 . "Plong")))) | |
(let ((results | |
(loop for c in cases | |
when (zerop (mod n (car c))) collect (cdr c)))) | |
(if results | |
(apply #'concatenate 'string results) | |
(write-to-string n)))) |
(I am annoyed that I never knew you commented here... )
I like your loop example. I'll take it and merge it into the pull request and we'll call this done.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By "single" I meant something like "lexical top-level", where the main body of the function is a single form. To me,
loop
anddo
are sophisticated enough that I often it's worth having them occupy the whole function. Anything that doesn't fit, goes in another function.I came up with these:
I'm not feeling gonzo enough to try one with
format
, but that may change later.