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.

## Patterns

In the following, we show various patterns of Egison.

### Wildcard

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

$identifier 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 $identifier_integer 

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

,expr

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

?predicate

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.

### Not-Patterns

!pattern

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

### And-Patterns

(& 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.

### Or-Patterns

(| pattern ...)

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

### 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.

### 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.

## 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 succeed, 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] ...})

### 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 this expression, see Matchers.