[racket] define-match-expander

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sat Dec 28 16:24:51 EST 2013

I'm not sure why this works and that doesn't, but try passing n as an  
argument to the match expander:

#lang racket

(require rackunit)

(define-match-expander num:
  (λ(stx)
    (syntax-case stx ()
      [(num: n)
       #'(? number? n)])))

(check-equal? (match 1 [(num: n) n]) 1)
(check-equal? (match 'x [(num: n) n] [_ 2]) 2)

I think it might have something to do with the Macro-Introduced  
Bindings section of the Syntax Model page in the documentation.

On Dec 26, 2013, at 2:45 PM, Jens Axel Søgaard wrote:

> The match pattern (? number? n) matches  number and
> binds it to n.
>
>> (match 1 [(? number? n) n])
>   1
>
> I'd like to write  (match 1 [n:num n]) instead.
>
> Since there is no define-identifier-match-expander I have
> tried to make (match 1 [(n:num) n]) work. I need a hint.
>
> Here is a non-working attempt:
>
> (define-match-expander n:num
>  (λ(stx)
>    (syntax-case stx ()
>      [(id)
>       (with-syntax ([n (syntax/loc #'id n)])
>         #'(? number? n))])))
>
>
> (check-equal? (match 1 [(n:num) n]) 1)
> (check-equal? (match 'x [(n:num) n] [_ 2]) 2)
>
> /Jens Axel
>
>
>
>
>
>
> --
> Jens Axel Søgaard
>
> ____________________
>  Racket Users list:
>  http://lists.racket-lang.org/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131228/2b9eac64/attachment.html>

Posted on the users mailing list.