Nordlund, Dan (DSHS/RDA) wrote:
-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of joseph.g.bo...@gsk.com
Sent: Friday, December 19, 2008 7:41 AM
To: Thomas Lumley
Cc: r-help@r-project.org
Subject: Re: [R] understanding lexical scope

Thomas, Jeff, Mark, Antonio,

Thank you for your answers. They have helped me clarify how R functions work. They work differently from SAS functions (which SAS calls macros.)

Well, SAS macros are not functions in the traditional sense. The SAS
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.
[....]


I bet one of the expeRts on the list will provide you with more detail
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.


--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalga...@biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to