[plt-scheme] example of match quasipattern

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Fri Aug 23 02:53:32 EDT 2002

Matthew Flatt wrote:
>   For list-related administrative tasks:
>     http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
>> I am trying to do something like
>>
>> (let ((sym 'duck))
>>     (match-lambda
>>       [(`, at sym . rest) rest]
>>       [else #f]))
>>
>> that would match (list 'duck) or (list 'duck ...).
>> Currently it matches any pair.
>
> I think the only escape back to Scheme within a pattern
> is `?':
>
>    (let ((sym 'duck))
>      (match-lambda
>       [((? (lambda (x) (equal? x sym))) . rest) rest]
>       [else #f]))

These matches witout quasipatterns work.

  > (match '(duck dog)
      [('duck rest) rest])
  dog

  > (match '(cat dog)
      [('duck rest) rest])
  . match: no matching clause for (cat dog)


The pattern 'duck matches the symbol duck.
Since the normal quasiquote behaves like this

  > (let ([sym 'duck])
      `(',sym dog))
  ('duck dog)

I  expected this to work:

  > (let ([sym 'duck])
      (match '(duck dog)
        [`(',sym rest) rest]))
  . match: no matching clause for (duck dog)

But it doesn't. I just wish I could see why.

--
Jens Axel Søgaard





Posted on the users mailing list.