# <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> ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-dumbbell fa-stack-1x fa-inverse"></i></span> Exercise: Linked List ADT - Open project `hw1` in VSCode, create a file `week2-patterns.sc` in folder `play` <div class="grid grid-cols-2 gap-4"> <div class="groupblock"> **Github Copilot** - Deactivate automatic suggestions - Add keyboard shortcut: `Cmd-Shift-P` - trigger inline suggestions - Chat commands <div class="text-sm"> - `/assess-exercise` - `/explain-code` - `/explain-compile-error` - `/find-resources` - `/infer-types` - `/make-exercise-plan` - `/step-exec` </div> </div> <div class="groupblock"> **Scala REPL** - Open a terminal - Open the Scala REPL: `sbt console` - Load and execute files `:load play/week2-patterns.sc` </div> </div> ---