real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Clojure

(No cheating in this version.)

Author:William James
Score: (3.02 in 324 votes)
(defn bottles [n & [capitalize]]
  (str (if (> n 0) n (if capitalize "No more" "no more"))
    " bottle" (if (= 1 n) "" "s") " of beer" ))

(defn bot-wall [n & cap] (str (bottles n cap) " on the wall"))

(defn sing
  ;  Default is 99 times.
  ([]  (sing 99))
    (doseq [i (range stock -1 -1)]
      (printf "%s, %s.\n%s.\n\n"
        (bot-wall i true) (bottles i)
        (apply str
          (if (> i 0)
            ["Take one down and pass it around, " (bot-wall (dec i))]
            ["Go to the store and buy some more, " (bot-wall stock)]


Download Source | Write Comment

Alternative Versions

More clojurisk and human readable versioThomas G. Kristensen03/08/100
JVM-based functional languageRavi Chugh / Zach Tatlock06/08/085


>>  Shamit Kumar Tomar said on 12/27/09 06:57:05

Shamit Kumar Tomar Nice and small code. Like it.

>>  Peter said on 01/30/10 23:11:37

Peter Isn't printf an invocation of Java? It doesn't look "clojury" to me. Java has proprietary formatting mechanism, but it also includes the printf method for its concise syntax.

>>  Rayne said on 03/30/10 07:52:26

Rayne printf uses Java's format under the hood. So what? "Where Java isn't broke, Clojure doesn't fix it." is a huge Clojure philosophy. This is perfectly 'Clojury'.

>>  Rayne said on 03/30/10 07:53:31

Rayne Except for putting the closing parens on a separate line. That's just stupid.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!




Security Code: