Programming ParadigmsBhavin Kamani
Cuisine
Music Genre
Imperative Programming
Instructional
Command
Variable State
Conditions
Loops
Direct
Efficient
Inflexible
Side Effects
Assembly, C
COBOL, Visual Basic
Object OrientedProgramming
Abstraction
Maintainibility
Learning Curve
Over Engineering
C++, Java, C#
Ruby, Python
Declarative Programming
"What" instead of "How"
Reduced Side effects
Order of statements not crucial
Domain Specific Languages
SQL
Domain Specific Languages
Regular Expression
Functional Programming
Composability
Concurrency
Learning Curve
Main stream support
LISP, Haskell, Erlang
Closure, Scala, F#
Multi-Paradigm Programming
http://www.99-bottles-of-beer.net
JavaScript
Lyrics99 bottles of beer on the wall, 99 bottles of beer.Take one down and pass it around, 98 bottles of beer on the wall.
98 bottles of beer on the wall, 98 bottles of beer.Take one down and pass it around, 97 bottles of beer on the wall.
97 bottles of beer on the wall, 97 bottles of beer.Take one down and pass it around, 96 bottles of beer on the wall.----------------------------------------------------------------------------------------------------------------------------------------------------
1 bottle of beer on the wall, 1 bottle of beer.Take one down and pass it around, no more bottles of beer on the wall.
No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.
Imperative Style
var lyrics = [];
for (var bottles = 99; bottles > 0; bottles--) { lyrics.push(bottles + " bottles of beer on the wall, " + bottles + " bottles of beer"); var next_bottles = ((bottles-1) == 0 ? "no more" : (bottles-1)); lyrics.push("Take one down and pass it around, " + next_bottles + " bottles of beer on the wall."); } var zero_bottle = "No more"; lyrics.push(zero_bottle + " bottles of beer on the wall, " + zero_bottle + " bottles of beer"); lyrics.push("Go to the store and buy some more, " + "99 bottles of beer on the wall.");
document.writeln(lyrics.join("<BR>"));
Object-Oriented Style var BottleSong = function(num_bottles, stanza){ this.num_bottles = num_bottles; this.stanza = stanza; }; BottleSong.prototype = { sing: function(separator){ var beer_form = " bottles of beer"; for (var bottles = this.num_bottles; bottles > 0; bottles--) { this.stanza.addLine(bottles + beer_form + " on the wall, " + bottles + beer_form); var next_bottles = ((bottles-1) == 0 ? "no more" : (bottles-1)); this.stanza.addLine("Take one down and pass it around, " + next_bottles + beer_form + " on the wall."); } var zero_bottle = "No more"; this.stanza.addLine(zero_bottle + beer_form + " on the wall, " + zero_bottle + beer_form); this.stanza.addLine("Go to the store and buy some more, " + this.num_bottles + beer_form + " on the wall."); return this.stanza.write(separator); } }
var Stanza = function(){ this.lines = []; }; Stanza.prototype = { addLine: function(line){ this.lines.push(line); },
write: function(separator) { return this.lines.join(separator); } }
var song = new BottleSong(99, new Stanza()); document.writeln(song.sing("<BR>"));
Functional Style var BottleSong = function(num_bottles){ var bottles = function(n){ return (n == 0 ? "no more" : n) + " bottles"; }
this.num_bottles = num_bottles; this.last_stanza = function(){ return ["No more bottles of beer on the wall, no more bottles of beer.", "Go to the store and buy some more, 99 bottles of beer on the wall."]; } this.stanza = function(n) { var line = [bottles(n) + " of beer on the wall, " + bottles(n) + " of beer."]; line.push("Take one down and pass it around, " + bottles(n-1) + " of beer on the wall."); return line; } }
BottleSong.prototype = { sing: function(separator){ var bottles = _.range(this.num_bottles,0,-1) var that = this; return _.reduce(bottles, function(lyrics, n) { return lyrics.concat(that.verse_n(n)); },[]).concat(that.verse_0()).join(separator); } }
var song = new BottleSong(99); document.writeln(song.sing("<BR>"));
bottles 0 = "no more bottles"bottles 1 = "1 bottle"bottles n = show n ++ " bottles"
verse 0 = "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."
verse n = bottles n ++ " of beer on the wall, " ++ bottles n ++ " of beer.\n" ++ "Take one down and pass it around, " ++ bottles (n-1) ++ " of beer on the wall.\n"
main = mapM (putStrLn . verse) [99,98..0]
Haskel Solution
(defn bottles-str [n] (str (cond (= 0 n) "no more bottles" (= 1 n) "1 bottle" :else (format "%d bottles" n)) " of beer"))
(defn print-bottle [n] (println (format "%s on the wall, %s." (bottles-str n) (bottles-str n))) (println "Take one down and pass it around," (bottles-str (dec n)) "on the wall.")) (defn sing [n] (dorun (map print-bottle (reverse (range 1 (inc n))))) (println "No more bottles of beer on the wall, no more bottles of beer.") (println "Go to the store and buy some more," (bottles-str n) "on the wall.")) (sing 99)
Clojure Solution
Piet Solution
Whitespace Solution
Paradigm Evolution
Java 8
Lambda Expression
Top Related