re: Daily Coding Puzzles - Nov 11th - Nov 16th VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

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)))))
code of conduct - report abuse