[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>)
result$a
# 0, say
copy = result
result$a = 1
result$a
# 1
copy$a
# ??
copy$b = 1
copy$b
# 1
result$b
# ??
as a concrete example, take base::srcfile. it returns an environment
tagged with "srcfile":
sf = srcfile("rubbish")
sf$filename
# rubbish
sfcopy = sf
sfcopy$filename = "hello dolly"
sf$filename
# 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:
is(sf)
# oops, "srcfile", but not "environment"
is.environment(sf)
# TRUE
or maybe investigate the source code of the function (where available):
srcfile
# ... 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.
vQ
More information about the R-help
mailing list