diff -r 4ffba2f72692 -r 244df77507c2 progs/lecture3.scala --- a/progs/lecture3.scala Sun Jul 14 14:31:39 2024 +0100 +++ b/progs/lecture3.scala Sun Sep 15 12:57:59 2024 +0100 @@ -347,6 +347,18 @@ for (n <- 1 to 20) println(fizz_buzz(n)) +// more interesting patterns for lists - calculate the deltas between +// elements + +def delta(xs: List[Int]) : List[Int] = xs match { + case Nil => Nil + case x::Nil => x::Nil + case x::y::xs => (x - y)::delta(y::xs) +} + +delta(List(10, 7, 8, 2, 5, 10)) + + // guards in pattern-matching def foo(xs: List[Int]) : String = xs match { @@ -391,6 +403,16 @@ fav_colour(Blue) +enum ChessPiece: + case Queen, Rook, Bishop, Knight, Pawn + def value = this match + case Queen => 9 + case Rook => 5 + case Bishop => 3 + case Knight => 3 + case Pawn => 1 + + // ... a tiny bit more useful: Roman Numerals