Next: , Previous: , Up: Scheme reference   [Contents][Index]


4.11 Pattern matching

The (hoot match) module provides a match form that supports a subset of what Guile’s (ice-9 match) can do. See the Guile manual for more detailed information.

Syntax: match exp clause1 clause2 ...

Match object exp against the patterns in clause1 clause2 … in the order in which they appear. Return the value produced by the first matching clause. If no clause matches, throw a match error.

Each clause has the form (pattern body1 body2 ...).

The following patterns are supported:

identifier

Matches anything and binds identifier.

_

Matches anything.

()

Matches the empty list.

#t

Matches #t.

#f

Matches #f.

'symbol

Matches a symbol.

fixnum

Matches a fixnum such as 42.

(not pattern)

Matches if sub-pattern does not match.

(and pattern ...)

Matches if every sub-pattern matches.

(or pattern ...)

Matches if any sub-pattern matches.

(? predicate)

Matches if (predicate exp) returns true.

(? predicate identifier)

Matches if (predicate exp) returns true and binds identifier.

(car-pattern . cdr-pattern)

Matches a pair if the car/cdr match their respective sub-patterns.

#(pattern ...)

Matches a vector of n elements if each element matches its respective sub-pattern.