Merge branch 'master' into more-encompassing-number-skips

This commit is contained in:
Arfon Smith
2015-07-29 13:54:51 +01:00
parent 885b5aab41
commit 90a293727d
434 changed files with 79876 additions and 56121 deletions
+40
View File
@@ -0,0 +1,40 @@
#!/bin/sh
exec scala "$0" "$@"
!#
object Beers extends Application {
def bottles(qty : Int, f : => String) = // higher-order functions
qty match {
case 0 => "no more bottles of beer" + f
case 1 => "1 bottle of beer" + f
case x => x + " bottles of beer" + f
}
def beers(qty : Int) = bottles(qty, " on the wall.")
def sing(qty : Int)(implicit song : String) : String = {
def takeOne =
qty match {
case 0 => "Go to the store and buy some more."
case x => "Take one down and pass it around."
}
def nextQty = // nested functions
if (qty == 0) 99
else qty - 1
def refrain = {
beers(qty).capitalize + " " + bottles(qty, "") + ".\n" +
takeOne + " " + beers(nextQty) + "\n\n"
}
if (qty == -1) song
else sing(qty - 1)(song + refrain) // tail recursion
}
implicit val headOfSong : String = ""
println(sing(99)) // implicit parameter
}