Skip to content

Instantly share code, notes, and snippets.

@verdammelt
Last active September 27, 2015 23:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verdammelt/6f2ba3ea7cdeeea9b38e to your computer and use it in GitHub Desktop.
Save verdammelt/6f2ba3ea7cdeeea9b38e to your computer and use it in GitHub Desktop.
raindrops implementation
(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))))
(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))))
@wobh
Copy link

wobh commented Aug 23, 2015

Pre-nitpick, I should try it, but I bet a single loop or do form would be sufficient.

Of course, I had something like this in mind:

(defparameter *raindrops*
  '((3 . "Pling")
    (5 . "Plang")
    (7 . "Plong"))
  "Frequency modulus and impact sound of raindrops.")

(defun convert (integer &optional (raindrops *raindrops*))
  "Raindrop sound or integer."
  (declare (type integer integer))
  (flet ((rainmaker (i)
           (with-output-to-string (str)
             (dolist (drop raindrops)
               (when (zerop (mod i (car drop)))
                 (princ (cdr drop) str))))))
    (let ((rain (rainmaker integer)))
      (if (zerop (length rain))
          (write-to-string integer)
          rain))))

@wobh
Copy link

wobh commented Aug 23, 2015

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.

@verdammelt
Copy link
Author

(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