[plt-scheme] Custom match expanders for eq?, eqv? and equal?

From: Sam TH (samth at ccs.neu.edu)
Date: Tue Aug 19 13:38:26 EDT 2008

I typically use `==' for this, to mean your `equal?'.  When I need a
different comparison, I just use `?', since that's pretty rare.

I would avoid changing the meaning of `eq?' etc, just because it
potentially makes it harder to read your code.

sam th

On Tue, Aug 19, 2008 at 1:00 PM, Dave Gurnell <d.j.gurnell at gmail.com> wrote:
> Hi PLTers,
> I've often thought an equivalent of "eq?" or "equal?" to be a useful
> addition for the match library. "?" does more or less what I want but it's a
> little verbose:
>   (define x 'a)
>   (define y 'b)
>   (match x
>     [(? (cut eq? <> y)) "X is the same as Y."])
> What I'd like to see is something like this:
>   (match x
>     [(eq? y) "X is the same as Y."])
> I had a play this afternoon and wrote the a module called "my-match.ss"
> (attached) that redefines eq?, eqv? and equal? to do what I've described
> above:
>   #lang scheme
>
>   (require (file "my-match.ss"))
>
>   ; and so on...
> Now for my question. By supplying my own versions of these identifiers, I
> have also changed how they are treated in an expression context (although
> the end result of using them should be the same). Is it a "Bad Thing" (TM)
> to do this to identifiers that are so fundamental to the core of Scheme?
> Should I rename these guys, or make sure I only ever require them with a
> prefix?
> Many thanks,
> -- Dave
> ; my-match.ss ====================================
> #lang scheme
> (require srfi/26)
> ; Match expanders --------------------------------
> ; (_ expr pattern ...)
> (define-match-expander match:eq?
>   (syntax-rules ()
>     [(_ value pattern ...)
>      (? (cut eq? <> value) pattern ...)])
>   eq?)
> ; (_ expr pattern ...)
> (define-match-expander match:eqv?
>   (syntax-rules ()
>     [(_ value pattern ...)
>      (? (cut eqv? <> value) pattern ...)])
>   eqv?)
> ; (_ expr pattern ...)
> (define-match-expander match:equal?
>   (syntax-rules ()
>     [(_ value pattern ...)
>      (? (cut equal? <> value) pattern ...)])
>   equal?)
> ; Provide statements -----------------------------
> (provide (rename-out [match:eq?    eq?]
>                      [match:eqv?   eqv?]
>                      [match:equal? equal?]))
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>



-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.