progs/lecture2.scala
changeset 266 ca48ac1d3c3e
parent 212 4bda49ec24da
child 268 e43f7e92ba26
equal deleted inserted replaced
265:59779ce322a6 266:ca48ac1d3c3e
   364 
   364 
   365 get_me_a_string(0)
   365 get_me_a_string(0)
   366 
   366 
   367 
   367 
   368 // you can also have cases combined
   368 // you can also have cases combined
   369 def season(month: String) = month match {
   369 def season(month: String) : String = month match {
   370   case "March" | "April" | "May" => "It's spring"
   370   case "March" | "April" | "May" => "It's spring"
   371   case "June" | "July" | "August" => "It's summer"
   371   case "June" | "July" | "August" => "It's summer"
   372   case "September" | "October" | "November" => "It's autumn"
   372   case "September" | "October" | "November" => "It's autumn"
   373   case "December" => "It's winter"
   373   case "December" => "It's winter"
   374   case "January" | "February" => "It's unfortunately winter"
   374   case "January" | "February" => "It's unfortunately winter"
   376  
   376  
   377 println(season("November"))
   377 println(season("November"))
   378 
   378 
   379 // What happens if no case matches?
   379 // What happens if no case matches?
   380 println(season("foobar"))
   380 println(season("foobar"))
       
   381 
       
   382 
       
   383 // Days of the months
       
   384 def days(month: String) : Int = month match {
       
   385   case "March" | "April" | "May" => 31
       
   386   case "June" | "July" | "August" => 30
       
   387 }
       
   388 
       
   389 
   381 
   390 
   382 
   391 
   383 // Silly: fizz buzz
   392 // Silly: fizz buzz
   384 def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
   393 def fizz_buzz(n: Int) : String = (n % 3, n % 5) match {
   385   case (0, 0) => "fizz buzz"
   394   case (0, 0) => "fizz buzz"