[racket] match and equality

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Sat Jul 10 18:03:48 EDT 2010

Hi all,

I have two questions regarding match. Hopefully both have an answer.

One of the things I would like to do in a match is to in the pattern
have a variable to stand for a given pattern and at the same time be
able to match on the structure of the variable.

For example, assume I want to match against a cons where the first
element is an a, and the second a list of b and c but I want to be
able to assign d to stand for the list of b and c.
Something like the following with some made up syntax:
(match (cons 1 (list 2 3))
   [(cons a (var d (list b c)))
    d])
> (2 3)

The second question is how to deal with two equal elements in a
pattern. For simple cases it seems I can do:
(match (cons 2 3)
  [(cons a a)
   'same]
  [(cons a b)
   'diff])

but what about if I want to use a specific equality predicate.
Something like (again with made up syntax):
(match (cons my-structure1 my-structure2)
  [(and (cons a a) (my-equal? a a))
   'same]
  [(cons a b)
   'diff])

Cheers,
-- 
PMatos


Posted on the users mailing list.