# [Maxima] Rules and patterns for dummies??

Robert Dodier robert.dodier at gmail.com
Sun Nov 1 17:44:30 CST 2009

```On 10/30/09, Julien Martin <balteo at gmail.com> wrote:

> Further to a prior post to me from another user, I would be extremely
> grateful if someone could give me a brief example of how to use rules and
> patterns.
> A super basic example/use case would be great!!

Not sure if this is still needed, but here is an example.

A rule, in Maxima, is just an identity, i.e. a declaration that
two different expressions are equivalent for values which
satisfy some predicate or predicates.

e.g. for positive integer values of n, foo(n) = n * foo(n - 1),
and foo(0) = 1.

tellsimpafter (foo (0), 1);
matchdeclare (nn, lambda ([e], integerp (e) and e > 0));
tellsimpafter (foo (nn), nn * foo (nn - 1));

Now we get this:

foo (x);
=> foo (x);
foo (3);
=> 3 * foo (2);
''%;
=> 6 * foo (1);
''%;
=> 6 * foo (0);
''%;
=> 6

There are probably apparently-obvious variations on this which
don't work. Also, maybe we'd like to have foo(3) => 6 immediately.
But instead of trying to figure out this example, maybe you can
say more about what you're trying to do. Sorry if this is old news,
I've been out of the loop.

HTH

Robert Dodier
```