Dissecting pattern matching in functional programming
In functional programming, pattern matching is a form of dispatch to choose the correct variant of the functions to be called. It's actually inspired by a standard mathematical notation with the syntax to express conditional execution. We can start our discussion on matching pattern by borrowing the code from Chapter 1, Tasting Functional Style in C#, when we talked about recursion. The following is the GetFactorial()
functional we used to retrieve a factorial value:
public partial class Program { private static intGetFactorial(intintNumber) { if (intNumber == 0) { return 1; } returnintNumber * GetFactorial(intNumber - 1); } }
As we can see in the preceding code, it gives us two definitions. In this case, the dispatcher is chosen based on whether the actual intNumber
parameter pattern matches 0 or not. The use of the pattern matching are closer to this if
conditional expression since we...