[racket] match and equality

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Sat Jul 10 18:18:02 EDT 2010

On Sat, Jul 10, 2010 at 6:03 PM, Paulo J. Matos <pocmatos at gmail.com> wrote:
> 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)

This is just `and':

> (match (cons 1 (list 2 3))
  [(cons a (and d (list b c)))
'(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])

See the `match-equality-test' parameter:

sam th
samth at ccs.neu.edu

Posted on the users mailing list.