Explaining pattern matching vs switch

15,152

Solution 1

Having formerly been one of "those people", I don't know that there's a succinct way to sum up why pattern-matching is such tasty goodness. It's experiential.

Back when I had just glanced at pattern-matching and thought it was a glorified switch statement, I think that I didn't have experience programming with algebraic data types (tuples and discriminated unions) and didn't quite see that pattern matching was both a control construct and a binding construct. Now that I've been programming with F#, I finally "get it". Pattern-matching's coolness is due to a confluence of features found in functional programming languages, and so it's non-trivial for the outsider-looking-in to appreciate.

I tried to sum up one aspect of why pattern-matching is useful in the second of a short two-part blog series on language and API design; check out part one and part two.

Solution 2

Patterns give you a small language to describe the structure of the values you want to match. The structure can be arbitrarily deep and you can bind variables to parts of the structured value.

This allows you to write things extremely succinctly. You can illustrate this with a small example, such as a derivative function for a simple type of mathematical expressions:

type expr =
    | Int of int
    | Var of string
    | Add of expr * expr
    | Mul of expr * expr;;

let rec d(f, x) =
    match f with
    | Var y when x=y -> Int 1
    | Int _ | Var _ -> Int 0
    | Add(f, g) -> Add(d(f, x), d(g, x))
    | Mul(f, g) -> Add(Mul(f, d(g, x)), Mul(g, d(f, x)));;

Additionally, because pattern matching is a static construct for static types, the compiler can (i) verify that you covered all cases (ii) detect redundant branches that can never match any value (iii) provide a very efficient implementation (with jumps etc.).

Solution 3

Excerpt from this blog article:

Pattern matching has several advantages over switch statements and method dispatch:

  • Pattern matches can act upon ints, floats, strings and other types as well as objects.
  • Pattern matches can act upon several different values simultaneously: parallel pattern matching. Method dispatch and switch are limited to a single value, e.g. "this".
  • Patterns can be nested, allowing dispatch over trees of arbitrary depth. Method dispatch and switch are limited to the non-nested case.
  • Or-patterns allow subpatterns to be shared. Method dispatch only allows sharing when methods are from classes that happen to share a base class. Otherwise you must manually factor out the commonality into a separate function (giving it a name) and then manually insert calls from all appropriate places to your unnecessary function.
  • Pattern matching provides redundancy checking which catches errors.
  • Nested and/or parallel pattern matches are optimized for you by the F# compiler. The OO equivalent must be written by hand and constantly reoptimized by hand during development, which is prohibitively tedious and error prone so production-quality OO code tends to be extremely slow in comparison.
  • Active patterns allow you to inject custom dispatch semantics.

Solution 4

Off the top of my head:

  1. The compiler can tell if you haven't covered all possibilities in your matches
  2. You can use a match as an assignment
  3. If you have a discriminated union, each match can have a different 'type'

Solution 5

Switch is the two front wheels.

Pattern-matching is the entire car.

Share:
15,152
mckurt
Author by

mckurt

Technical Consultant/QGIS Specialist at Digital Mapping Solutions. Passionate and enjoy spreading "the good news" about using open source GIS :) QGIS, PostGIS and Python are my current weapons of choice. Current Projects: QGIS : Contributor GitHub : Owner

Updated on June 06, 2022

Comments

  • mckurt
    mckurt almost 2 years

    I have been trying to explain the difference between switch statements and pattern matching(F#) to a couple of people but I haven't really been able to explain it well..most of the time they just look at me and say "so why don't you just use if..then..else".

    How would you explain it to them?

    EDIT! Thanks everyone for the great answers, I really wish I could mark multiple right answers.

  • Francesco De Vittori
    Francesco De Vittori over 13 years
    Good example. When explaining pattern matching to "non-functional people" I like to mention that with PM your condition can check the "shape" of your data, which is much harder, ugly and inefficient with if/switch.
  • Totti
    Totti about 13 years
    "experiential". Good word. :-)
  • Lukasz Madon
    Lukasz Madon over 11 years
    Add of expr * expr I think you meant to write +
  • Bruno De Fraine
    Bruno De Fraine over 11 years
    No lukas, this is abstract syntax, not concrete syntax, so + is invalid and * should not be interpreted as arithmetic multiplication. Add of expr * expr declares a non-constant constructor Add whose 2 arguments both have type expr. So you use it to construct a combination of 2 expressions that is labelled as an Add.