[plt-scheme] Applying DRY in macros

From: Jens Axel Soegaard (jensaxel at soegaard.net)
Date: Fri Sep 12 19:38:47 EDT 2008

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




Posted on the users mailing list.