Some Useful Patterns
8.11.1

Some Useful Patterns

The match form can do a lot in a small amount of space, but it can be confusing, especially if you don’t have experience using a "pattern-matching" feature.

Here are a bunch of the things that match can do. Take a look at each one, and make sure you understand what’s going on. Some of the examples are deliberately weird. See if you can understand what’s going on with the weird ones, too.

thing to match

pattern

anything

foo

anything

bar

the symbol'cat

'cat

the symbol'dog

'dog

a real number

(? real?)

a real number

(? real? zz)

a real number

(? real? r)

a list

(? list? l)

a list

(list l ...)

a list of two things

(list a b)

a list of two symbols

(list (? symbol? a) (? symbol? b))

a list of symbols

(list (? symbol? items) ...)

a list containing a real, the symbol 'dog, and a bunch of symbols

(list (? real? r) 'dog (? symbol? s) ...)