Fixing Broken Macros with Eval and Quasiquote
March 21, 2009 at 8:20 pm | In coding, lisp, scheme, tutorial | 2 CommentsTags: eval, macro, scheme
Recently, I found myself needing to deal with a “convenience” macro, which quoted several of its arguments for me before passing them along to the real function. Unfortunately, only the macro was exported from the library, and I was unable to access the base function.
(define-syntax convenient-function (syntax-rules () ((_ arg1 arg2) (much-harder-function 'arg1 'arg2))))
How useful. To save me a handful of quotes, I lose the ability to programmatically generate my arguments.
As I was loath to reimplement the entire library just to regain that ability, I looked for another solution. Thankfully, I found one.
(define (much-harder-function arg1 arg2) (eval `(convenient-function ,arg1 ,arg2) (environment '(convenient-lib))))
As far as I can tell, this circumvents the macro’s quoting features entirely to allow me to pass in a dynamically generated symbol. While it won’t work with functions which modify the environment, it worked well in my case, and allowed me to move on to more interesting code.
2 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
Why wasn’t the function in scope of the macro?
Comment by Grant Rettke — March 22, 2009 #
The function wasn’t visible because I was dealing with an R6RS library which only exported the macro. I couldn’t just modify the library because it was a standard library for the implementation, and I would prefer not to modify those if I can help it, as it hinders upgrades quite a bit.
Comment by Will Donnelly — March 23, 2009 #