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.
You can't. Macros are expanded by the Preprocessor, which happens even before the code is compiled. It is a purely textual replacement. If you need to change something at runtime, just replace your macro with a real function call.
The first parameter symbol is %1 , the second is %2 , and so on. You pass values for the parameters when you reference the macro symbol name for expansion. Macro parameter values are character sequences of no formal type, and they are comma delimited. There is no way to pass in a comma as part of a parameter value.
A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word. The file extension of a macro is commonly . MAC.
Overview. Macro in C programming is known as the piece of code defined with the help of the #define directive. Macros in C are very useful at multiple places to replace the piece of code with a single value of the macro. Macros have multiple types and there are some predefined macros as well.
I do not think that you can do something like this with macros. I also do not see any point in trying.
Please note that macros are not just some functions with extra magic! Macros are something different altogether.
Perhaps you are looking for something like method combinations in Common Lisp?
With macros doing this you are on the way to create the hardest to maintain software on the planet.
Edit: in Common Lisp it is possible. I can't remember that I have ever seen it used in source code.
Adding behavior to functions or macros is often called 'advise' or 'advice' in the Common Lisp community. Some 'advise' tools have also the ability to advise macros.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With