21 Match as a Switch
Pattern matching will be familiar to anyone coming from a Haskell background, however it might look a bit weird at first for anyone coming from imperative languages.
Pattern matching in it’s most basic form, can look like an extended switch, with some differences:
- The
matchkeyword comes after the variable (selection matchcompared toswitch(selection) - There is no need for
break; - There is no fall through
- default case is
case _(orcase xwhere x can be any lower case identifier, more about it in the next slides)
Pattern matching can be very powerful beyond a switch replacement, and will be explained in later slides