I often find the following type of incremental definition useful:
(define (foo) (display "bar"))
(foo)
;prints bar
(define foo (let ((bar foo))
(lambda ()
(display "foo")
(bar))))
(foo)
;prints foobar
How do I preform this type of incremental definition with macros?
I could not get let-syntax to provide the same functionality.
Currently I use plt scheme, but would like to see answers in different lisp implementations as well.
Edit:
Naively I would want to do the following:
(define-syntax foo
(syntax-rules ()
((_) (display "bar"))))
(define-syntax foo
(let-syntax ((old-foo (syntax-rules () ((_) (foo)))))
(syntax-rules ()
((_) (begin
(display "foo")
(old-foo))))))
Translation of naive macros to working plt scheme macros:
(require-for-syntax scheme/base)
(define-syntax foo
(syntax-rules ()
[(foo) (display "bar")]))
(define-syntax foo
(let ([old (syntax-local-value #'foo)])
(lambda (stx)
#`(begin #,((syntax-rules ()
[(_) (begin (display "foo"))]) stx)
#,(old #'(_))))))
(foo)
If I am missing a better method let me know.
FWIW (and this is definitely not much, since this is pretty much an exercise in target practicing with your feet), here is how you would do this in PLT Scheme with only hygienic
syntax-rulesmacros:This will not work inside a module, only on the REPL — and that is a good thing. It is possible to do something similar in modules too, but if you’re going for that, you can just as well use procedural macros (aka
syntax-casemacros), with an identifier that is bound at the syntax level and `set!’-ing its value to extend it. Still not a great idea, and can still lead to eyes bleeding, but some people like to hurt themselves…(Oh, and BTW — even doing this is still completely unrelated to whether the macros in question are hygienic or not.)