99 Bottles of Beer in Clojure

Programmers, in their continual search for popular acceptance, have made writing a program to generate the lyrics of 99 Bottles of Beer into somewhat of an advanced “Hello World” exercise. I did it today in Clojure and it took me about 5 minutes. There’s a repository of programs written in 1270 programming languages.

(beer.clj) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
; 99 bottles of beer in Clojure
; Kanwei Li, 2009

(defn inflect-bottle [time]
  (cond (= time 1) "1 bottle of beer"
        (= time 0) "no more bottles of beer"
        :else (str time " bottles of beer")))

(defn sing [time]
  (if (= time 0)
    (println (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."))
    (let [inflected (inflect-bottle time)
          next-time (- time 1)]
      (println (str inflected " on the wall, " inflected
                    ".\nTake one down and pass it around, "
                    (inflect-bottle next-time) " on the wall.\n"))
      (recur next-time))))

(sing 99)
Posted on