The Assert and AssertList Objects

The Assert and AssertList objects are the two objects most commonly used in clauses in functions. The left hand side is always a pattern match, the right-hand side is an expression built using the variables bound in the pattern match.

Clauses built with the Assert object

When the left hand side of an Assert is evaluated, it returns a list of variable bindings if the pattern match is successful. Otherwise, the clause evaluates to null. For example, suppose we have a function named Commute with a single clause:

and we execute the function Commute[x + y]. The pattern matching process will succeed in this case and will bind "x" to the variable "a" and "y" to the variable "b".

If we tried to execute Commute[x - y], then the pattern matching process would fail because the Sub object doesn't match the Add in the pattern. In this case, the clause would return null and the function would also return null if this is the only clause contained in the function.

If the pattern match succeeds, then the right side is built using the values bound to each variable.

Patterns that generate more than one set of bindings

The section on Pattern Matching to Monoids describes these patterns in detail.

Clauses that use the AssertList object

Clauses that are built using the AssertList object typically contain pattern match terms that can generate more than one list of bindings. When the AssertList object is used, the right-hand side of the AssertList object is evaluated for each binding lists generated by the pattern match term. Each time that right-hand side of the AssertList object generates an expression from a binding list, that expression is added to the List object that is returned by the clause. For example, the clause

generates the following list: