How to resolve the algorithm Comma quibbling step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Comma quibbling step by step in the Clojure programming language

Table of Contents

Problem Statement

Comma quibbling is a task originally set by Eric Lippert in his blog.

Write a function to generate a string output which is the concatenation of input words from a list/sequence where:

Test your function with the following series of inputs showing your output here on this page:

Note: Assume words are non-empty strings of uppercase characters for this task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Comma quibbling step by step in the Clojure programming language

Source code in the clojure programming language

(defn quibble [sq]
  (let [sep (if (pos? (count sq)) " and " "")]
    (apply str
      (concat "{" (interpose ", " (butlast sq)) [sep (last sq)] "}"))))

; Or, using clojure.pprint's cl-format, which implements common lisp's format:
(defn quibble-f [& args]
  (clojure.pprint/cl-format nil "{~{~a~#[~; and ~:;, ~]~}}" args))

(def test
  #(doseq [sq [[]
               ["ABC"]
               ["ABC", "DEF"]
               ["ABC", "DEF", "G", "H"]]]
     ((comp println %) sq)))

(test quibble)
(test quibble-f)


  

You may also check:How to resolve the algorithm Multiplication tables step by step in the zkl programming language
You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Lua programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Elixir programming language
You may also check:How to resolve the algorithm Calkin-Wilf sequence step by step in the Scheme programming language
You may also check:How to resolve the algorithm Monads/List monad step by step in the Julia programming language