[plt-scheme] Applying DRY in macros

From: Anthony Cowley (acowley at seas.upenn.edu)
Date: Fri Sep 12 20:21:19 EDT 2008

Does that one do the right thing? I thought it would have been phrased more
like...
(define-syntax (for3 stx)
  (syntax-case stx ()
    ((for x in/as y body ...)
     (let-values (((element list) (cond
                                    ((free-identifier=? #'in/as #'in)
                                     (values #'x #'y))
                                    ((free-identifier=? #'in/as #'as)
                                     (values #'y #'x))
                                    (else (error 'for "Invalid syntax")))))
       #`(map (lambda (#,element) body ...) #,list)))))


Anthony


On Fri, Sep 12, 2008 at 7:38 PM, Jens Axel Soegaard
<jensaxel at soegaard.net>wrote:

> danprager at optusnet.com.au wrote:
>
>> In the reddit comments about the tutorial
>>
>>
>> http://www.reddit.com/r/programming/comments/710el/a_scheme_syntaxrules_primer/
>>
>> the following refactoring was suggested
>>
>> (define-syntax for
>>  (syntax-rules (in as)
>>    ((for element in list body ...)
>>     (map (lambda (element)
>>            body ...)
>>          list))
>>    ((for list as element body ...)
>>     (for element in list body ...))))
>>
>> which does eliminate the template duplication, but repeats the line
>>  (for element in list body ...)
>>
>> first as a pattern, then as a template.
>>
>> Can anyone suggest further improvements?
>>
>
> (define-syntax (for stx)
>  (syntax-case stx ()
>   [(for element in/as list body ...)
>    (or (free-identifier=? #'in/as #'in)
>        (free-identifier=? #'in/as #'as))
>    #'(map (lambda (element) body ...) list)]))
>
>
> --
> Jens Axel Søgaard
>
>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080912/a5ac9e36/attachment.html>

Posted on the users mailing list.