[plt-scheme] matching/dispatching a la Erlang

From: William Sprague (wwsprague at ucdavis.edu)
Date: Thu Aug 28 00:57:59 EDT 2003

Hi Schemers,

I would like to branch based on unification, like Erlang, so I can get a
"case" form on steroids, which would bind free variables and match
structure and constants, ("destructuring"?).  I was wondering if I should
be using match.ss to do this.  If so, could someone point me to a simpler
example than an implementation of the Y-operator?  (The Y-operator.  Wow. 
It's kind of funny.)

I want to be able to do something like the following, where
"case-on-steroids" is my desired operator:

(define (choose input)
  (case-on-steroids (X Y)       ; define X and Y as free variables
    ((X X Y) (+ X Y))           ; matches (input 1 1 2), bind X->1, Y->2
    ((1 X (2 Y) X) (* X Y))     ; matches (input 1 100 '(2 300) 100)
                                ; X->100, Y->300
    (_ 'whatever))))            ; matches anything no bindings

My model for this is the pattern matching way of defining procedures in
Erlang, which, in turn, is derived from Prolog.  

It looks like define-syntax has pattern matching like I want, and having
fenders would be great too, but I want to use the facility as a glorified
case statement.

Any pointers would be much appreciated.  If match.ss can do this, I
apologize for giving up on the documentation.  

Thanks in advance,
Webb Sprague
Programmer III
Wildlife Fish Conservation Biology 
University of California at Davis
One Shields Avenue
Davis, CA 95616

Posted on the users mailing list.