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)))) |
By "single" I meant something like "lexical top-level", where the main body of the function is a single form. To me, loop
and do
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:
;;; loop
(defun convert (integer &optional (raindrops *raindrops*))
"String of integer or raindrop sound."
(declare (type integer integer))
(loop
for (div . sound) of-type (integer . string) in raindrops
when (zerop (mod integer div))
collect sound into sounds
finally
(return
(if sounds
(format nil "~{~A~}" sounds)
(write-to-string integer)))))
;;; do
(defun convert (integer &optional (raindrops *raindrops*))
"String of integer or raindrop sound."
(declare (type integer integer))
(do* ((rain raindrops (rest rain))
(drip (first rain) (first rain))
(drop ()))
((endp drip)
(if drop
(format nil "~{~A~}" (nreverse drop))
(write-to-string integer)))
(when (zerop (mod integer (car drip)))
(push (cdr drip) drop))))
;;; sequence
(defun convert (integer &optional (raindrops *raindrops*))
"String of integer or raindrop sound."
(declare (type integer integer))
(flet ((divisiblep (div)
(zerop (mod integer div)))
(stringcat (str1 str2)
(concatenate 'string str1 str2)))
(let ((drops (remove-if-not #'divisiblep raindrops :key #'car)))
(if drops
(reduce #'stringcat drops :key #'cdr)
(write-to-string integer)))))
I'm not feeling gonzo enough to try one with format
, but that may change later.
(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
Pre-nitpick, I should try it, but I bet a single
loop
ordo
form would be sufficient.Of course, I had something like this in mind: