# [plt-scheme] matching/dispatching a la Erlang

William Sprague wrote:
>* 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
*
Here is my take on your example:
(define (choose input)
(match input
[(1 x (2 y)) (* x y)]
[(x1 x2 y) (=> fail) (if (not (= x1 x2))
(fail)
(+ x1 y))]
[_ 'whatever]))
(choose '(3 3 5))
(choose '(1 3 5))
(choose '(1 3 (2 7)))
If there is an easier way to express that the two values
called x1 and x2 above must be equal - I'd like to see it.
--
Jens Axel Søgaard