progs/lecture3.scala
changeset 374 90b267768329
parent 366 1c829680503e
child 375 ddda2e76a40f
equal deleted inserted replaced
373:d29cd5883c7b 374:90b267768329
   191 parse_date("2019-11-26")
   191 parse_date("2019-11-26")
   192 parse_date("26/11/2019")
   192 parse_date("26/11/2019")
   193 parse_date("26.11.2019")
   193 parse_date("26.11.2019")
   194 
   194 
   195 
   195 
   196 
   196 // guards in pattern-matching
   197 
   197 
       
   198 def foo(xs: List[Int]) : String = xs match {
       
   199   case Nil => s"this list is empty"
       
   200   case x :: xs if x % 2 == 0
       
   201      => s"the first elemnt is even"
       
   202   case x :: y :: rest if x == y
       
   203      => s"this has two elemnts that are the same"
       
   204   case hd :: tl => s"this list is standard $hd::$tl"
       
   205 }
       
   206 
       
   207 foo(Nil)
       
   208 foo(List(1,2,3))
       
   209 foo(List(1,2))
       
   210 foo(List(1,1,2,3))
       
   211 foo(List(2,2,2,3))
   198 
   212 
   199 // Tail recursion
   213 // Tail recursion
   200 //================
   214 //================
   201 
   215 
   202 
   216