[plt-scheme] On macros and Scheme

From: Majorinc, Kazimir (kazimir at chem.pmf.hr)
Date: Wed Dec 19 05:14:56 EST 2007

Joshua Zucker wrote:
> I enjoy the blog "Good Math, Bad Math", and the latest installment is
> particularly relevant to us:
>   http://scienceblogs.com/goodmath/2007/12/macros_why_theyre_evil.php
>
> Basically he says that macros are evil, and some are eviller than
> others, but, well, maybe Scheme gets it right, and if only there were
> a macro debugger ...
>
> I think these PLT guys have something figured out, maybe :)
>   
Macro Stepper is really good one, but author is right, macros are bad, 
they are hard to debug, macro expansion is very space and time consuming 
- unsuitable for runtime generated code, not first-class citizens ... 
Macro semantics and syntax of macro call is still more handy/powerful 
than functions, but with few I'd say, feasible improvements in Scheme 
semantics, functions could do everything macros can. Look at this two 
functions:

(define (display-expression-and-value expr env)
  (parameterize ((current-namespace env))
                (display "(evaluates-to ")
                (display expr)
                (display " ")
                (display (eval expr))
                (display ") \n")))

(define (++ var env)
  (parameterize ((current-namespace env))
                (eval `(set! ,var (+ (eval ,var) 1)))))

(define x 4)
(display-expression-and-value '(+ x x) (current-namespace))
(++ 'x (current-namespace))
(display x)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;(evaluates-to (+ x x) 8)
;5

This is typically done by macros, namespaces make it possible by 
function. Unfortunately, syntax of the function call in this example is 
quite a bit uglier and eval does not recognize all variables - but 
really only few small steps are left from this point to making macros 
obsolete.




Posted on the users mailing list.