[plt-scheme] Algebraic patterns

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Oct 1 13:47:13 EDT 2002

On Oct  1, Robert Bruce Findler wrote:
> Wow, this looks great!

Thanks!  I do think it's much better than some eopl-like cases thing
to destruct things.  If I were teaching, I would definitely use this
approach...  (Oh, one more missing thing to make it actually be
pattern matching, is to be able to try matches instead of just getting
an error...)

> One thought: most of those forms add syntax to places that were illegal
> already; eg (let ([(list x y) (list 1 2)]) x) was illegal before and so
> it seems good to change it in that way. The one that doesn't do that is
> define. In your world, this:
>   (define (id x) x)
> would probably be an error instead of defining the identity function.
> So, I wonder if it would be better to only stick with adding meaning to
> things that were illegal before and support patterns like this:
>   (define (id--on-pairs (cons x y)) (cons x y))
> but not patterns like this:
>   (define (cons hd tl) (cons 1 2))
> You might have with a new define-pattern form for the above, tho.

Well, first of all, define would work as usual for everything except
defined patterns, so `(define (id x) x)' works as long as `id' is not
defined as a pattern.  For those cases, you can just use `define' with
an explicit lambda which will always work.  I thought about making
those pattern identifiers different which is bad because it doesn't
look like the constructor any more.  I thought about your solution too
but if you go that way, then you could also do the implicit lists and
vector thing from match.ss.

> One other thought: you could also add support for curried function
> patterns:
>   (define ((adder x) y) (+ x y))
>   (define add3 (adder 3))

Heh, of course -- it'll probably take me some time, but at some point
I will add this thing to Swindle, where I already made the above
possible.  I even went further and allowed this:

  => (let* ((((adder x) y) (+ x y)) (add3 (adder 3))) (add3 4))

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!

Posted on the users mailing list.