I’m reading about functional programming and I’ve noticed that Pattern Matching is mentioned in many articles as one of the core features of functional languages.
Can someone explain for a Java/C++/JavaScript developer what does it mean?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Understanding pattern matching requires explaining three parts:
Algebraic data types in a nutshell
ML-like functional languages allow you define simple data types called “disjoint unions” or “algebraic data types”. These data structures are simple containers, and can be recursively defined. For example:
defines a stack-like data structure. Think of it as equivalent to this C#:
So, the
ConsandNilidentifiers define simple a simple class, where theof x * y * z * ...defines a constructor and some data types. The parameters to the constructor are unnamed, they’re identified by position and data type.You create instances of your
a listclass as such:Which is the same as:
Pattern matching in a nutshell
Pattern matching is a kind of type-testing. So let’s say we created a stack object like the one above, we can implement methods to peek and pop the stack as follows:
The methods above are equivalent (although not implemented as such) to the following C#:
(Almost always, ML languages implement pattern matching without run-time type-tests or casts, so the C# code is somewhat deceptive. Let’s brush implementation details aside with some hand-waving please 🙂 )
Data structure decomposition in a nutshell
Ok, let’s go back to the peek method:
The trick is understanding that the
hdandtlidentifiers are variables (errm… since they’re immutable, they’re not really “variables”, but “values” 😉 ). Ifshas the typeCons, then we’re going to pull out its values out of the constructor and bind them to variables namedhdandtl.Pattern matching is useful because it lets us decompose a data structure by its shape instead of its contents. So imagine if we define a binary tree as follows:
We can define some tree rotations as follows:
(The
let rotateRight = functionconstructor is syntax sugar forlet rotateRight s = match s with ....)So in addition to binding data structure to variables, we can also drill down into it. Let’s say we have a node
let x = Node(Nil, 1, Nil). If we callrotateLeft x, we testxagainst the first pattern, which fails to match because the right child has typeNilinstead ofNode. It’ll move to the next pattern,x -> x, which will match any input and return it unmodified.For comparison, we’d write the methods above in C# as:
For seriously.
Pattern matching is awesome
You can implement something similar to pattern matching in C# using the visitor pattern, but its not nearly as flexible because you can’t effectively decompose complex data structures. Moreover, if you are using pattern matching, the compiler will tell you if you left out a case. How awesome is that?
Think about how you’d implement similar functionality in C# or languages without pattern matching. Think about how you’d do it without test-tests and casts at runtime. Its certainly not hard, just cumbersome and bulky. And you don’t have the compiler checking to make sure you’ve covered every case.
So pattern matching helps you decompose and navigate data structures in a very convenient, compact syntax, it enables the compiler to check the logic of your code, at least a little bit. It really is a killer feature.