<div dir="ltr"><div><div><div>I think it isn't really predictable and you should always just use syntax->list.<br><br>But in the first case, when you write a pattern like<br><br></div> (x y ...)<br><br></div>and you have an earlier syntax binding like you do, then the pattern matching macros see this as a 'cons'-like operation on syntax and so you get a pair of syntax objects.<br>
<br></div>Exploring the source locations of the intermediate "cons"es inside can also help shed light on what's happening any particular example, too.<br><br>Robby<br><div><br></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Mon, May 27, 2013 at 2:52 PM, Tim Nelson <span dir="ltr"><<a href="mailto:tbnelson@gmail.com" target="_blank">tbnelson@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Thanks for the quick reply, and the helpful example!<br>
<br>
Can you say a bit more about the circumstances under which syntax-e<br>
will return a pair, even if its datum is a list? That's the biggest<br>
cause of confusion for me.<br>
<br>
For my purposes, I may just end up switching to syntax->list in my<br>
program, since that will always return a list.<br>
<div class="HOEnZb"><div class="h5"><br>
On Mon, May 27, 2013 at 3:27 PM, Jay McCarthy <<a href="mailto:jay.mccarthy@gmail.com">jay.mccarthy@gmail.com</a>> wrote:<br>
> A syntax object contains a lot of information in addition to the datum<br>
> inside it. Sometimes that datum will be a pair or a list. And<br>
> sometimes the extra data needs to be associated with just the pair and<br>
> sometimes with all the elements of the list.<br>
><br>
> If you look at this program:<br>
><br>
> #lang racket/base<br>
> (require (for-syntax racket/base<br>
> racket/match))<br>
> (begin-for-syntax<br>
> (define show-syntax<br>
> (match-lambda<br>
> [(? syntax? s)<br>
> (printf "syntax from line ~a\n"<br>
> (syntax-line s))<br>
> (show-syntax (syntax-e s))]<br>
> [(? list? l)<br>
> (printf "list\n")<br>
> (for-each show-syntax l)]<br>
> [(? pair? p)<br>
> (printf "pair\n")<br>
> (show-syntax (car p))<br>
> (show-syntax (cdr p))]<br>
> [(? symbol? s)<br>
> (printf "symbol ~a\n" s)])))<br>
><br>
> (define-syntax (ex stx)<br>
> (show-syntax stx)<br>
> #'(void))<br>
><br>
> (define-syntax (helper1 stx)<br>
> #`(ex #,stx))<br>
><br>
> (define-syntax (helper2 stx)<br>
> (syntax-case stx ()<br>
> [(_ a ...)<br>
> (syntax<br>
> (ex a ...))]))<br>
><br>
> (define-syntax (helper3 stx)<br>
> (syntax-case stx ()<br>
> [(_ a ...)<br>
> (syntax<br>
> (ex a ... z))]))<br>
><br>
> (module+ main<br>
> (ex x)<br>
> ex<br>
> (helper1 a b c)<br>
> (helper1 . (a b c))<br>
> (helper2 a b c)<br>
> (helper3 a b c))<br>
><br>
> You can see how each of the different "helpers" appear to do things<br>
> that are very close to one another but turn out to have different<br>
> syntax representations.<br>
><br>
> You normally don't need to worry about this, but sometimes you need to<br>
> pay attention to it when you want to get correct source code<br>
> origination information. (Basically, if you have a list in the pattern<br>
> input and do something like creating a new syntax object like #'(a<br>
> ...) in a macro and then put that in, then you'll get source code<br>
> location information from the macro and not from the source of the<br>
> list, which sometimes gets exposed.)<br>
><br>
> Jay<br>
><br>
><br>
> On Mon, May 27, 2013 at 1:06 PM, Tim Nelson <<a href="mailto:tbnelson@gmail.com">tbnelson@gmail.com</a>> wrote:<br>
>> Dear All,<br>
>><br>
>> The documentation for syntax-e says that it may return many different<br>
>> types, including a syntax-pair, but it doesn't really describe why. Here's<br>
>> a concrete piece of code:<br>
>><br>
>> (define stx<br>
>> (with-syntax ([x #'5]<br>
>> [(y ...) #'(1 2)])<br>
>> (syntax (x y ...))))<br>
>><br>
>> (syntax->datum stx) returns a list.<br>
>><br>
>> (syntax->list stx) (of course) returns a list, too.<br>
>><br>
>> But (syntax-e stx) returns a pair! If I do this:<br>
>><br>
>> (define stx2<br>
>> (with-syntax ([x #'5]<br>
>> [y #'(1 2)])<br>
>> (syntax (x y))))<br>
>><br>
>> I get a list from syntax-e, but of course the nesting is wrong/different.<br>
>><br>
>> So my question is: why a pair?<br>
>><br>
>> Best,<br>
>> - Tim (Running Racket v. 5.3.3)<br>
>> ____________________<br>
>> Racket Users list:<br>
>> <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
><br>
><br>
><br>
> --<br>
> Jay McCarthy <<a href="mailto:jay@cs.byu.edu">jay@cs.byu.edu</a>><br>
> Assistant Professor / Brigham Young University<br>
> <a href="http://faculty.cs.byu.edu/~jay" target="_blank">http://faculty.cs.byu.edu/~jay</a><br>
><br>
> "The glory of God is Intelligence" - D&C 93<br>
____________________<br>
Racket Users list:<br>
<a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</div></div></blockquote></div><br></div>