Pattern Matching - Overview

The purpose of a pattern match is to bind an expression to a variable during the execution of code. Most commonly, the expressions that become the values bound variables are the parameters passed into a function call.

Simple variables

The simplest pattern matching might be something like

{ x + y => v }

where v is in the Variables namespace. In this case, the variable v is bound to the expression x + y. Normally, variables are rendered in blue to make it easier to distinguish variables from other expressions.

Variables with parameters

Variables can also be constructed with parameters. For example, we can create this match:

{ x + y => f(a, b) }

Here we f, a and b are all in the Variables namespace and don't have object definitions (if you just type "f", etc. in a clause under an object defintion, the objects you create are automatically in the Variables namespace). In this case, there are three bindings created.

a = x

b = y

f = Add

By default, undefined terms (like "x") created in expressions in Object Modules are assigned to the Variables namespace since that is what’s most commonly intended. You can change the namespace of these expressions if needed.