Summary
In my Adventures I have referred many times to pattern matching, but only in the context of compile time pattern matching in macros. There is another form of pattern matching,
which is quite common in Scheme and in other
functional languages: run time pattern matching. This episode will shed some light on the technique.

It is impossible to overvalue the importance of pattern matching which
is in my opinion one of the most important concepts in programming.
Unfortunately, this technique is only available in very high level
programming languages and therefore it is usually unknown to the
average programmer.

I saw pattern matching for the first time in '95,
when using Mathematica for High Energy Physics symbolic
computations, which is a very specific usage indeed.
Nowadays, however, the trend toward higher
and higher abstraction is influencing all programmming languages and I
am pretty sure than soon or later pattern matching will enter in
mainstream languages.

For the moment, you can find it in functional languages and, in a poor
man form, in certain scripting languages. It should be noticed that
common functional languages such as SML, OCaml, F#, Haskell (or even
Scala) only have run time pattern matching, since they lack macros. In
Scheme instead compile time pattern matching is somewhat preferred:
you use it to manipulate compile-time lists which are actually blocks
of code wrapped in macros.

Runtime pattern matching is used to
manipulate lists (or other data structures) which are only know at
runtime: in particular, they could be user input. Compile time pattern
matching can be used to implement a compiler; run time pattern matching
to implement an interpreter.

In this episode I will discuss only a poor man form of runtime
pattern matching, list destructuring, i.e. the ability to
match (nested) lists with a single predefined pattern. This ability is
akin to what def-syntax can do at compile time. Full runtime pattern
matching is able to manage a whole set of patterns, akin to what
syntax-match can do at compile time.

The poor form of pattern matching is called tuple
unpacking in Python (note for lispers: you would call it destructuring bind).
For instance, you can write:

Tuple unpacking works at any level of nesting and for any kind of
iterable, therefore it is pretty powerful. Moreover, tuple unpacking
is even more powerful in Python 3.0, where it is possible to
split an iterable into its head (car) and tail (cdr):

>>> head, *tail=(i for i in (1,2,3))
>>> (head, tail)
(1, [2, 3])

I have noticed in episode #5
that the star syntax in Python is similar to the dot syntax
in Scheme, when used in the signature of functions with a
variable number of arguments (variadic functions);
the syntactic extension in Python 3.0 makes the similarity
stronger.

The main difference between Python and Scheme is that Scheme
pattern matching is not polymorphic, i.e. you cannot match
with the same pattern a list and a vector or an equivalent
iterable. You must use different patterns, or esplicitely convert
the types.

There are plenty of libraries
for full runtime pattern matching: one of the most common is the
match library by Andrew Wright, which is available practically
for all Scheme implementations. In Chicken Scheme match is actually
built-in in the core language:

Recently the implementation of match has been rejuvenated by Alex Shinn, who
fixed a few bugs and reimplemented everything in terms of
syntax-rules macros, whereas the original used define-macro:
this modern
implementation is also available as an R6RS library, thanks to
Derick Eddington and you can download it for here, if you want to
use this matcher with Ikarus.

Studying the documentation of match is certainly a good use of your
time, and a recommended reading; on the other hand, writing your
own matcher relying on Scheme macros is even more interesting.
In the next paragraph
I will implement a let+ macro with the full power of
tuple unpacking, and in future episodes I will implement a fully fledged
list matcher.

This paragraph will use a test-first approach, and will begin
with a specification of how
let+ is intended to work by means of tests.
I am using here the
minimal testing framework I have introduced in episode #11.
Here are the tests:

In the first case there an argument (y) in excess, not matched by any
element; in the second case, there is an element (3) in excess,
not matched by any argument. The implementation also checks (at compile time)
that the passed arguments are valid identifiers:

The error message is clear, we also know that a car was involved,
but unfortunately, it does not give
much information about where exactly the error happened :-(
I was serious at the end of episode #12, when I said that
debugging macros is no fun: the problem is that the errors
happen in expanded code which is invisible to the programmer.

In the next episode I will give a few examples of usage of let+ which
will make clear why it is so useful. See you next time!