Skip to content
Prev 165246 / 398506 Next

understanding lexical scope

Nordlund, Dan (DSHS/RDA) wrote:
macro language for the most part just does text substitution prior to
the SAS code being sent to the SAS "compiler"/interpreter. So, your
description of rewriting the "function body" in step 1. below, is fairly
accurate for SAS macro, but it is not accurate for R. If you try to fit
R functions into a SAS macro language mold you will only confuse
yourself on both accounts. I will leave the technical details of R
functions to the R experts.
[....]
than could have ever hoped for.


Not much, I think. It's one of those cases where you too easily end up 
rewriting manuals or even books. The text above is quite accurate: 
Macro-based languages substitute text, structured languages call 
functions with parameters. And some do a bit of each. And every now and 
again you wish that the language at hand would do the opposite of what 
it actually does.

One distinction is if you have things like

#define f(x) 2*x
#define g(y) f(y+2)

(in the C language preprocessor syntax), then you end up with g(y) as 
y+2*2 (i.e., y+4), whereas the corresponding function calls give 
2*(y+2). Also, and the flip side of the original question: Macros have 
difficulties with encapsulation; with a bit of bad luck, arguments given 
to f() can modify its internal variables.

In R there are things that you want to do that are macro-like, and you 
can generally achieve the same effect with substitute/match.call/eval 
constructions, but it does get a bit contorted (lines 3-10 of the lm 
function is required reading if you want to understand these matters). 
Some of us occasionally ponder whether it would be cleaner to have a 
real (LISP-style) macro facility, but nothing really convincing has come 
up this far.