[R] Summary: do.call and environments

Thomas Lumley tlumley at u.washington.edu
Thu Mar 11 16:47:07 CET 2004


On Thu, 11 Mar 2004, Liaw, Andy wrote:

> Gabor,
>
> > From: Gabor Grothendieck
> >
> > Note that R and S are fundamentally different when it comes to
> > scoping.
> >
> > R uses lexical scoping, i.e. the parent environment of a function
> > is the environment at the point where it is *defined* whereas
> > S uses dynamic scoping, i.e. the parent environment in a function
> > is the environment at the point where the function is *called*.
>
> I don't think that's quite right.  S does not use dynamic scope.  This
> simple example fails in S-PLUS (6.1):

I'm not sure if there is standard jargon for the scoping rules that S
uses, but it certainly isn't dynamic scope.  The scope of a name does
not change: it's always the local frame plus the global workspace, so we
have static scope.

One way to describe the difference between S and R scoping is that in S
environment(fun) would always be the global workspace (frame 0), which is
why the environment() function isn't needed in S.

As a final note, it is "obvious" that dynamic scope is useful and easy to
implement, so people often try to fake it. Lisp developers also used to
think that dynamic scope was more useful and easier than dynamic scope.
They don't now.


	-thomas




More information about the R-help mailing list