progs/lecture3.scala
changeset 374 90b267768329
parent 366 1c829680503e
child 375 ddda2e76a40f
--- a/progs/lecture3.scala	Fri Nov 27 00:55:24 2020 +0000
+++ b/progs/lecture3.scala	Fri Nov 27 15:03:50 2020 +0000
@@ -193,8 +193,22 @@
 parse_date("26.11.2019")
 
 
+// guards in pattern-matching
 
+def foo(xs: List[Int]) : String = xs match {
+  case Nil => s"this list is empty"
+  case x :: xs if x % 2 == 0
+     => s"the first elemnt is even"
+  case x :: y :: rest if x == y
+     => s"this has two elemnts that are the same"
+  case hd :: tl => s"this list is standard $hd::$tl"
+}
 
+foo(Nil)
+foo(List(1,2,3))
+foo(List(1,2))
+foo(List(1,1,2,3))
+foo(List(2,2,2,3))
 
 // Tail recursion
 //================