[plt-scheme] matching/dispatching a la Erlang

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Aug 28 09:24:08 EDT 2003

Look up match in Help Desk and then follow the links in "libraries."

BTW, your terminology is wrong:
   matching : when you have a pattern and a term and you compute a
	substitution (a table of pattern-variable/value associations) that
	makes them equal
   unification: when you have two patterns and you compute a substitution
	that makes them equal

-- Matthias, the person who brought matching from Prolog to Scheme in 
to the laughter of Bruce Duba who then optimized it and I am forever 
grateful :-)

On Thursday, August 28, 2003, at 12:57 AM, William Sprague wrote:

>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 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.