# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Pattern Matching - Pattern matching vs. Projections <div class="grid grid-cols-2 gap-4"> <div> - Decomposition with pattern matching ```scala def f (xs: List[(Int,String)]) = xs match case Nil => "List is empty" case x :: Nil => s"List has one element: $x" case _ :: (x,_) :: _ => s"The second int is $x" end f val zs = List ((11,"dog"), (21,"cat"), (31,"sloth")) f(zs) // 21 ``` </div> <div> * Decomposition with projections ```scala def f (xs: List[(Int,String)]) = if xs == Nil then "List is empty" else if xs.tail == Nil then s"List has one element: ${xs.head}" else s"The second int is ${xs.tail.head(0)}" end f val zs = List ((11,"dog"), (21,"cat"), (31,"sloth")) f(zs) // 21 ``` </div> </div> ---