[plt-scheme] Custom match expanders for eq?, eqv? and equal?
Carl Eastlund
cce at ccs.neu.edu
Tue Aug 19 18:06:49 EDT 2008
Could we add eq?, eqv?, and equal? as builtin match bindings, the way
we already interpret list and cons as patterns? Is there a way for
users to add match interpretations for existing bindings? Its a weird
use of syntax, but it has its uses... it would be nice to say, for
instance, (make-posn x y) instead of (struct posn (x y)). Or, for
that matter, (list* a b) instead of (list-rest a b). Right now, if a
constructor exists without the forethought of also adding it as a
match expander, we're out of luck.
--Carl
On Tue, Aug 19, 2008 at 1:38 PM, Sam TH <samth at ccs.neu.edu> wrote:
> 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?]))
>>
More information about the plt-scheme
mailing list