[gambit-list] Request: examples of cool uses of macros

Álvaro Castro-Castilla alvaro.castro.castilla at gmail.com
Fri Jan 25 09:59:34 EST 2013


There are millions, of course, but this one (taken from:
http://c2.com/cgi/wiki?DefineSyntax), might be a good example of a DSL
that makes lisp almost "english". Not very useful for real-life, but I
think may be a good illustrative example:

  (define-syntax replace
    (syntax-rules (initially with until just-before)
      ((replace <var> initially <value> with <newvalue> until <done>)
        (let loop ((<var> <value>))
          (if <done> <var> (loop <newvalue>))))
      ((replace <var> initially <value> with <newvalue> until
just-before <done>)
        (let loop ((old #f) (<var> <value>))
          (if <done> old (loop <var> <newvalue>))))
      ((replace <var1> <var2> initially <value1> <value2> with
<newvalue1> <newvalue2> until <done>)
        (let loop ((<var1> <value1>) (<var2> <value2>))
          (if <done> (list <var1> <var2>)
            (loop <newvalue1> <newvalue2>))))))

  (replace x initially 1 with (* x 2) until (> x 1000)) => 1024

  (replace x y initially 1 1 with y (+ x y) until (> x 1000)) => (1597 2584)

  (replace x initially 1 with (* x 2) until just-before (> x 1000)) => 512





On Fri, Jan 25, 2013 at 3:47 PM, Jason Felice <jason.m.felice at gmail.com> wrote:
>
> Hi!
>
> Does anyone have any examples of macros which drastically improve
> readability or simplicity of code?  Maybe neat hacks?
>
> Not deep hacks that take a long time to understand, but things which can be
> used to illustrate why macros are powerful and awesome.
>
> Background:
>
> I'm giving a talk tomorrow on Scheme to prisoners in a job training program.
> The purpose is to expand their brains by exposing them to concepts they
> haven't seen before (they don't have Internet access).
>
> The talk is about Scheme and "Code is Data".  I've laid the foundation, but
> am getting to the point where I want to have some flashy and interesting
> uses of macros.
>
> Any help would be appreciated.
>
> Thanks,
> -Jason
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>


More information about the Gambit-list mailing list