<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>I just wrote a match-expander that does something like that:</div><div><br></div><div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 1 [(my-pat n:num) n]) 1)</font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 'x [(my-pat n:num) n] [_ 2]) 2)</font></div></div><div><br></div><div>like this:</div><div><br></div><div><font class="Apple-style-span" face="'Courier New'">#lang racket</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><div><font class="Apple-style-span" face="'Courier New'">(require rackunit)</font></div><div><font class="Apple-style-span" face="'Courier New'">(require (for-syntax</font></div><div><font class="Apple-style-span" face="'Courier New'"> (only-in lang/htdp-intermediate-lambda</font></div><div><font class="Apple-style-span" face="'Courier New'"> string-contains?)</font></div><div><font class="Apple-style-span" face="'Courier New'"> </font><span class="Apple-style-span" style="font-family: 'Courier New'; ">racket/string</span></div><div><font class="Apple-style-span" face="'Courier New'"> racket/match))</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(define-match-expander my-pat</font></div><div><font class="Apple-style-span" face="'Courier New'"> (lambda (stx)</font></div><div><font class="Apple-style-span" face="'Courier New'"> (syntax-case stx ()</font></div><div><font class="Apple-style-span" face="'Courier New'"> [(my-pat pat)</font></div><div><font class="Apple-style-span" face="'Courier New'"> (let* ([pat-sym (syntax->datum #'pat)]</font></div><div><font class="Apple-style-span" face="'Courier New'"> [pat-str (symbol->string pat-sym)])</font></div><div><font class="Apple-style-span" face="'Courier New'"> (cond [(not (string-contains? ":" pat-str))</font></div><div><font class="Apple-style-span" face="'Courier New'"> #'pat]</font></div><div><font class="Apple-style-span" face="'Courier New'"> [else</font></div><div><font class="Apple-style-span" face="'Courier New'"> (parse-pat-str pat-str stx)]))])))</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(define-for-syntax (parse-pat-str pat-str stx)</font></div><div><font class="Apple-style-span" face="'Courier New'"> (let ([split-pat (string-split pat-str ":")])</font></div><div><font class="Apple-style-span" face="'Courier New'"> (match split-pat</font></div><div><font class="Apple-style-span" face="'Courier New'"> [(list pat-name-str type-str)</font></div><div><font class="Apple-style-span" face="'Courier New'"> (with-syntax ([type-pred (type-str->stx-type-pred type-str)]</font></div><div><font class="Apple-style-span" face="'Courier New'"> [pat-name (datum->syntax stx (string->symbol pat-name-str))])</font></div><div><font class="Apple-style-span" face="'Courier New'"> #'(? type-pred pat-name))])))</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(define-for-syntax (type-str->stx-type-pred type-str)</font></div><div><font class="Apple-style-span" face="'Courier New'"> (match type-str</font></div><div><font class="Apple-style-span" face="'Courier New'"> ["num" #'number?]</font></div><div><font class="Apple-style-span" face="'Courier New'"> ["str" #'string?]</font></div><div><font class="Apple-style-span" face="'Courier New'"> ["lst" #'list?]))</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 1 [(my-pat n:num) n]) 1)</font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 'x [(my-pat n:num) n] [_ 2]) 2)</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match "string" [(my-pat s:str) s]) "string")</font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 'x [(my-pat s:str) s] [_ 2]) 2)</font></div><div><font class="Apple-style-span" face="'Courier New'"><br></font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match (list 1 2 3) [(my-pat l:lst) l]) (list 1 2 3))</font></div><div><font class="Apple-style-span" face="'Courier New'">(check-equal? (match 'x [(my-pat l:lst) l] [_ 2]) 2)</font></div></div><div><br></div><div><br></div><br><div><div>On Dec 26, 2013, at 2:45 PM, Jens Axel Søgaard wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>The match pattern (? number? n) matches number and<br>binds it to n.<br><br><blockquote type="cite">(match 1 [(? number? n) n])<br></blockquote> 1<br><br>I'd like to write (match 1 [n:num n]) instead.<br><br>Since there is no define-identifier-match-expander I have<br>tried to make (match 1 [(n:num) n]) work. I need a hint.<br><br>Here is a non-working attempt:<br><br>(define-match-expander n:num<br> (λ(stx)<br> (syntax-case stx ()<br> [(id)<br> (with-syntax ([n (syntax/loc #'id n)])<br> #'(? number? n))])))<br><br><br>(check-equal? (match 1 [(n:num) n]) 1)<br>(check-equal? (match 'x [(n:num) n] [_ 2]) 2)<br><br>/Jens Axel<br><br><br><br><br><br><br>--<br>Jens Axel Søgaard<br><br>____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></div></blockquote></div><br></body></html>