This is the mail archive of the
kawa@sourceware.org
mailing list for the Kawa project.
RE: pretty-printing?
David,
> Ok thanks! Since I mostly used gambit before, I'm more used to define-
> macro
> style macros! Lets say, for example, that I want to have an ugly
> imperative
> style 'for macro like:
>
> (define-macro (for init limit . exps)
>
> (let ((var (gensym 'i))
>
> (loop (gensym 'loop)))
>
> `(let ,loop ((,var ,init))
>
> (if (< ,var ,limit)
>
> (begin ,@exps
>
> (,loop (+ ,var 1)))))))
>
> then, in the define-syntax style, would the macro:
>
> (define-syntax for
> (syntax-rules ()
> ((_ init limit exp1 . exps)
> (let loop ((var init))
> (if (< var limit)
> (begin (begin exp1 . exps)
> (loop (+ var 1))))))))
First, with a hygienic macro, you usually supply the variable name
explicitely. Otherwise, 'var' will be new unique symbol that you won't be
able to reference in your expressions.
And use the "..." notation to match a (possibly empty) list of expressions:
(define-syntax for
(syntax-rules ()
((_ var init limit exp1 exps ...)
(let loop ((var init))
(if (<= var limit)
(begin
(begin exp1 exps ...)
(loop (+ var 1))))))))
#|kawa:1# (for x 1 10 (display x) (newline))
1
2
3
4
5
6
7
8
9
Dominique