Hi Terry,
Terry Therneau wrote:> I am currently puzzled by a passage in the R Extensions manual, section
5.10:
>
> SEXP lapply(SEXP list, SEXP expr, SEXP rho)
> {
> R_len_t i, n = length(list);
> SEXP ans;
>
> if(!isNewList(list)) error("`list' must be a list");
> if(!isEnvironment(rho)) error("`rho' should be an
environment");
> PROTECT(ans = allocVector(VECSXP, n));
> for(i = 0; i < n; i++) {
> defineVar(install("x"), VECTOR_ELT(list, i), rho);
> SET_VECTOR_ELT(ans, i, eval(expr, rho));
> }
>
> I'm trying to understand this code beyond just copying it, and
don't find
> definitions for many of the calls. PROTECT and SEXP have been well
discussed
> previously in the document, but what exactly are
> R_len_t
> defineVar
this function defines the variable (SYMSXP; one type of SEXP) of
its first argument, to have the value given by its second argument, in
the environment defined by its third argument. There are lots of
variants, these are largely in envir.c
> install
all symbols in R are unique (there is only one symbol named x, even
though it might have bindings in many different environments). So to get
the unique "thing" (a SYMSXP) you call install (line 1067 in names.c
has
a pretty brief comment to this effect). This makes it efficient to do
variable look up, as we only need to compare pointers (within an
environment), not compare names.
> VECTOR_ELT
access the indicated element (2nd arg) of the vector (first arg)
> SET_VECTOR_ELT
set the indicated element (2nd arg), of the vector (1st arg) to
the value (3rd arg)
>
> The last I also found in 5.7.4, but it's not defined there either.
>
> So:
> What do these macros do? Some I could guess, like is.Environment; and
I'm
> fairly confident of R_len_t. Others I need some help.
> Perhaps they are elswhere in the document? (My version of acrobat
can't do
> searches.) Is there another document that I should look at first?
> Why "isNewList"? I would have guessed "isList".
What's the difference?
"old lists" are of the CAR-CDR variant, and largely only used
internally these days. "new lists", are generic vectors, and are what
users will almost always encounter (even users that program internals,
you pretty much need to be messing with the language itself to run into
the CAR-CDR variety).
best wishes
Robert
>
> Thanks for any help,
> Terry Therneau
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
--
Robert Gentleman, PhD
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
PO Box 19024
Seattle, Washington 98109-1024
206-667-7700
rgentlem at fhcrc.org