equal
deleted
inserted
replaced
345 } |
345 } |
346 |
346 |
347 for (n <- 1 to 20) |
347 for (n <- 1 to 20) |
348 println(fizz_buzz(n)) |
348 println(fizz_buzz(n)) |
349 |
349 |
|
350 // more interesting patterns for lists - calculate the deltas between |
|
351 // elements |
|
352 |
|
353 def delta(xs: List[Int]) : List[Int] = xs match { |
|
354 case Nil => Nil |
|
355 case x::Nil => x::Nil |
|
356 case x::y::xs => (x - y)::delta(y::xs) |
|
357 } |
|
358 |
|
359 delta(List(10, 7, 8, 2, 5, 10)) |
|
360 |
|
361 |
350 // guards in pattern-matching |
362 // guards in pattern-matching |
351 |
363 |
352 def foo(xs: List[Int]) : String = xs match { |
364 def foo(xs: List[Int]) : String = xs match { |
353 case Nil => s"this list is empty" |
365 case Nil => s"this list is empty" |
354 case x :: xs if x % 2 == 0 |
366 case x :: xs if x % 2 == 0 |
388 case Green => true |
400 case Green => true |
389 case _ => false |
401 case _ => false |
390 } |
402 } |
391 |
403 |
392 fav_colour(Blue) |
404 fav_colour(Blue) |
|
405 |
|
406 enum ChessPiece: |
|
407 case Queen, Rook, Bishop, Knight, Pawn |
|
408 def value = this match |
|
409 case Queen => 9 |
|
410 case Rook => 5 |
|
411 case Bishop => 3 |
|
412 case Knight => 3 |
|
413 case Pawn => 1 |
|
414 |
393 |
415 |
394 |
416 |
395 // ... a tiny bit more useful: Roman Numerals |
417 // ... a tiny bit more useful: Roman Numerals |
396 |
418 |
397 sealed abstract class RomanDigit |
419 sealed abstract class RomanDigit |