Do you think on something like:
(define-macro (evenm? n) (zero? (% (eval n) 2)))
I think macros should be used only if one cannot use functions. The functions are slightly shorter, faster (in this case 20%) and probably easier to manipulate by meta-code. For example,
for with existing syntax cannot be defined as function because
(for (i 1 100) ... ) would return error trying to evaluate
(i 1 100). Logical operation
or can be defined as function, but if you want
or to be lazy, i.e. it stops evaluating after first non-nil argument, then you need macros.
In this case, I think function is better choice.
Why easier to manipulate by meta-code? For example, I wrote
debug-wrap to help me with debugging.
Code: Select all
(load "http://www.instprog.com/Instprog.default-library.lsp")
(define (evenf? n) (zero? (% n 2)))
(define-macro (evenm? n) (zero? (% (eval n) 2)))
(debug-wrap evenf?)
(debug-wrap evenm?)
(set 'x 44)
(evenf? x)
; Output:
;
; (evenf? (in 44)
; (out true)); t=0, mem=3, sym=0
;
(evenm? x)
; (evenm? (in x)
; (out true)); t=0, mem=3, sym=0
;
(exit)
Macro output is less useful because
evenm? really recives
x as argument, and not evaluated
x as
evenf?.