Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Scala

(Fun semi-DSL readable version)

Date:01/31/08
Author:Calum Leslie
URL:http://www.zootm.co.uk
Comments:0
Info:http://www.scala-lang.org
Score: (2.96 in 28 votes)
object NinetyNineBottles {
  def main(args: Array[String]) = {
    
    for (verse <- (1 to 99).reverse.map(verse _)) {
      println(verse)
      println
    }
    
    println(line1(0))
    println("Go to the store and buy some more, " + (99 bottles) + " of beer on the wall.")
  }
  
  def verse(n: Int) = line1(n) + '\n' + line2(n)
      
  def line1(n: Int) = 
    (n bottles).capitalize + " of beer on the wall, " + (n bottles) + " of beer."
    
  def line2(n: Int) = 
    "Take one down, pass it around, " + ((n-1) bottles) + " of beer on the wall."

  implicit def intToBottlesInt(i: Int) = new BottlesInt(i)
}

class BottlesInt(i: Int) {
  def bottles = i match {
    case 0 => "no more bottles"
    case 1 => "1 bottle"
    case n => n + " bottles"
  }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
Animated versionD Mackenzie10/02/090
Scala with a vengeanceEduardo Costa09/16/090
GrabbaBeer & CaseOfBeersD Mackenzie05/30/090

Comments

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!

Name:

eMail:

URL:

Security Code:
  
Comment: