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

TOP OF THREAD FULL DISCUSSION
re: Wednesday - Format words into a sentence (6 KYU) Complete the method so that it formats the words into a single comma separated value. The last w...
 

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