[R] R and Scheme

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Dec 10 15:30:44 CET 2008

Wacek Kusnierczyk wrote:
> Stavros Macrakis wrote:
>> There is no equivalent to set-car!/rplaca (not even pairlists and
>> expressions).  For example, r<-pairlist(1,2); r[[1]]<-r does not create a
>> circular list. And in general there doesn't seem to be substructure sharing
>> at the semantic level (though there may be in the implementation).
> computations on environment objects seem not to be subject to the
> copy-value-on-assignment semantics:
> e <- new.env(parent=emptyenv())
> ee <- e
> e$a <- 0
> ee$a

there's actually more to environments than that.  the issue is, you can
make a function that returns an environment disguised as an object of
some custom class, and it will behave like an environment rather than
according to the assign-to-copy way.  the issue is, unless you know
whether the object is an environment or not, you cannot tell what happen
in code like the following:

result = foo(<some args>)
# 0, say

copy = result
result$a = 1
# 1

# ??

copy$b = 1
# 1
# ??

as a concrete example, take base::srcfile.  it returns an environment
tagged with "srcfile":

sf = srcfile("rubbish")
# rubbish

sfcopy = sf
sfcopy$filename = "hello dolly"
# hello dolly

the man page makes no effort to make you know, it does not mention
environments at all:

"The 'srcfile' function produces an object of class 'srcfile', which
contains the name and directory of a source code file, along with its
timestamp, for use in source level debugging (not yet implemented) and
source echoing."

you'd have to pay attention to check yourself:

# oops, "srcfile", but not "environment"


or maybe investigate the source code of the function (where available):

# ... e <- new.env(...)
# ...
# class(e) <- "srcfile"
# return(e)

the point is, you'd better not make assumptions about how your code will
perform unless you check what the return values from the functions you
use really are, because it has impact on how assignments are made.


More information about the R-help mailing list