DEV Community

Discussion on: Daily Coding Puzzles - Nov 11th - Nov 16th

Collapse
 
gypsydave5 profile image
David Wickes • Edited

Common Lisp

Inhumane format version:

(defun format-words (words)
  (format nil "~{~#[~;~a~;~a and ~a~:;~@{~a~#[~;, and ~:;, ~]~}~]~}" words))

appropriated from Practical Common Lisp


More humane version (kinda like a string builder but using a stream):

(defun format-words (words)
  (with-output-to-string (s)
    (format s "~{~a~^, ~}" (butlast words))
    (unless (= 1 (length words))
      (format s " and "))
    (format s "~a" (car (last words)))))