Patterns occupy important positions in Egison. They are distinguished from the other objects, and form their own rich world. This chapter explains various kinds of patterns and syntax using patterns. To explain the syntax and its operational semantics, we give simple Egison code as examples. Since these chapters are so simple, you perhaps cannot understand the details of Egison. Then, we strongly recommend you to read other chapters after reading this chapter. The readers who want to master pattern-matching should read the next chapter Pattern Matching. If you want to create your own matcher, see the chapter Matchers.


In the following, we show various patterns of Egison.


Here, we explain the special pattern wildcard. _ represents a wildcard. Any object matches this pattern.

Pattern Variables


We can bind values to variables in pattern-matching. For this purpose, we use pattern variables. Any object matches this pattern, and then the variable is locally bound to the object.

Indexed Pattern Variables


Indexed pattern variables are special pattern variables. In this pattern, the variable is regarded as an hash, and then the matching object is bound to the value of the hash associated the integer key.

Inductive Patterns

<identifier value ...>

Patterns can be defined inductively. The notation of inductive patterns is similar to that of inductive data. The exception is, in inductive patterns, the head of an identifier is lowercase. The behaviour of inductive patterns is defined in a matcher. For eample, the cons pattern constructor is used to divide a collection into an element and the rest collection. The behaviour of inductive pattern cons is defined in each matcher.

The join pattern constructor is used to divide a collection into two collections.

Finally, the nil is a pattern constructor that takes no arguments and matches if a collection is empty.

Value Patterns


A value pattern is a pattern that matches with objects equal with the content of the pattern. The expression following , can be any kind of expressions.
The equality is defined in each matcher, and then varies by matcher. We shouldn't forget to place , ahead of the pattern.

Predicate Patterns


A predicate pattern is a pattern that matches with objects which satisfy the predicate following ?. The expression following ? should be a function that takes an argument and return a boolean value.


(& pattern ...)

An and-pattern is a pattern that matches the object, if and only if, all of the patterns are matched.

We can use and-patterns like as-patterns. For example, any non-empty collection matches the following and-pattern, and then the variable xs is bound to it.


(| pattern ...)

An or-pattern matches with the object, if the object matches one of given patterns.



A not-pattern matches with a object if the object does not matches the pattern following after !.

Later Patterns

Warning: Later pattern is deprecated since version 3.10.2. Please use sequential pattern instead.

(later pattern)

A later pattern is used to change the order of the pattern-matching process. Basically, our pattern-matching system processes patterns from left to right in order. However, we sometimes want this order, for example, to refer to the value bound to the right side of pattern variables. A later pattern can be used for such purpose.

Sequential Patterns

{pattern ...}

A sequential pattern is a generalization of a later pattern. A sequential pattern is represented as a list of patterns. Pattern matching is executed for each pattern in order. # that appears in a sequential pattern is called later pattern variable. The target data bound to later pattern variables are pattern-matched in the next sequence. When multiple later pattern variables appear, they are pattern-matched as a tuple in the next sequence. It allows us to apply not-patterns for different parts of a pattern at the same time.

Loop Patterns

Loop patterns are used to express patterns that contains loops and the number of loops changes by the parameters. Please read the next chapter for detail.

Let Patterns

(let {[variable expr] ...} pattern)

This is similar to the let expressions. Each variable is locally bound to each expr, and the variables can be used in the pattern.

Syntax Related with Patterns

match-all Expressions

(match-all target-expr matcher-expr [pattern expr])

match-all expressions take a single match-clause. match-all expressions return a collection that contains the evaluated value of expr with each result of pattern-matching.

We have explained about this expression in Concept of Egison.
In the next chapter, there are many examples.

match Expressions

(match target-expr matcher-expr {[pattern expr] ...})

The difference from match-all expressions is match expressions take multiple match-clauses. It tries pattern-matching for each pattern from the head of match-clauses. When pattern-matching succeeds, the expr of the match-clause will be evaluated. Pattern-matching of Egison has multiple results. However, this expression chooses the first result and they are locally bound and then the formula will be evaluated with them.

Please also check the Poker Hands demonstration.

match-lambda Expressions

(match-lambda matcher-expr {[pattern expr] ...})

Like a lambda expression, a match-lambda expression creates a function with an argument. Pattern matching is done with the argument. Roughly speaking, (match-lambda X {[Y Z] ...}) and (lambda [$x] (match x X {[Y Z] ...})) are the same.

match-all-lambda Expressions

(match-all-lambda matcher-expr [pattern expr])

Like a mach-lambda expression, a match-all-lambda expression creates a function with an argument. Pattern matching is done with the argument. Roughly speaking, (match-all-lambda X [Y Z]) and (lambda [$x] (match-all x X [Y Z])) are the same. This expression has a simgle match-clause as a match-all expression.

pattern-function Expressions

(pattern-function [variable ...] pattern)

A pattern-function is a function that gets only patterns and returns a pattern. We define it using a pattern-function expression. It allows us to reuse useful patterns.

Please see the following examples. Since a pattern-function has lexical scoping as a normal function by lambda, bindings for pattern-variables in the argument patterns and the body of pattern-functions don't conflict. Then, we don't have to care about which pattern-variable occurs in a pattern-function.

Although Egison restricts use of patterns in a match-clause, what you can do isn't limited at all.

matcher Expressions

This expression is used to create a matcher. Since this is complicated, we don't explain this expression here, but do in another chapter. If you want to know how to use this expression, see Matchers.

What to do next...

Next Chapter: Pattern Matching Top of Manual Back to Home