src/beer_song.clj

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
(ns beer-song)

(defn n-bottles [n]
  "Correctly describe / pluralize n bottles"
  (if (= n 1) "1 bottle" (str n " bottles")))

(defn current-bottles [n]
  "Sing about the current lot of n bottles"
  (str (n-bottles n) " of beer on the wall, " (n-bottles n) " of beer.\n"))

(defn next-bottles [n]
  "Sing about the next lot of n bottles"
  (cond
    (= n 0) "Take it down and pass it around, no more bottles of beer on the wall.\n"
    :else (str "Take one down and pass it around, " (n-bottles n) " of beer on the wall.\n")))

(defn verse [n]
  "Sing the nth verse"
  (cond
    (= n 0) (str "No more bottles of beer on the wall, no more bottles of beer.\n"
                 "Go to the store and buy some more, 99 bottles of beer on the wall.\n" "")

    :else (str (current-bottles n) (next-bottles (- n 1)))))

(defn sing
  "Sing a range of verses"
  ([start] (sing start 0))
  ([start end] (apply str
                      (interpose "\n"
                                 (for [n (range start (- end 1) -1)] (verse n))))))

Comments


You're not logged in right now. Please login via GitHub to comment