[R] Sending "..." to a C external

Emmanuel Charpentier emm.charpentier at free.fr
Fri Aug 22 23:04:33 CEST 2008


Le vendredi 22 août 2008 à 15:16 -0400, John Tillinghast a écrit :
> I'm trying to figure this out with "Writing R Extensions" but there's not a
> lot of detail on this issue.
> I want to write a (very simple really) C external that will be able to take
> "..." as an argument.
> (It's for optimizing a function that may have several parameters besides the
> ones being optimized.)

!!! That's a hard one. I have never undertaken this kind of job, but I expect 
that your "..." argument, if you can reach it from C (which I don't know) will 
be bound to a Lisp-like structure, notoriously hard to decode in C. Basically,
you'll have to create very low level code (an duplicate a good chunk of the R
parser-interpreter...). 

I'd rather treat the "..." argument in a wrapper that could call the relevant 
C function with all arguments interpreted and bound... This wrapper would
probably be an order of magnitude slower than C code, but two orders of magnitude
easier to write (and maintain !). Since "..." argument parsing would be done *once*
before the "grunt work" is accomplished by C code, the slowdown would (probably)
be negligible...

> I got the "showArgs" code (from R-exts) to compile and install, but it only
> gives me error messages when I run it. I think I'm supposed to pass it
> different arguments from what I'm doing, but I have no idea which ones.
> 
> What exactly are CAR, CDR, and CADR anyway? Why did the R development team
> choose this very un-C-like set of commands? 

'Cause they are fundamental to the Lisp-like language that is S/R. Read on...

>                                             They are not explained much in
> R-exts.

At the risk of incurring the R-help deities wrath :

"In the beginning, John Mc Carthy created Lisp 1.5, who begat MACLISP who begat 
..., who begat Scheme ... ". Nonwhistanding a long inheritance story, full of sound,
fury, holy wars and bastardry (sometimes evoking European royalty lines...), all 
Lisp-like language bear one mark (dominant allele) of their common ancestor, which
is the list structure. This structure is implemented as a pair of pointers, the
first one  pointing to the first element of the list, the second pointing to ...
the list of the rest of the members (or nothing, aka NIL).

In McCarthy's implementation, which was machine code on an IBM 704 (we were in 1957, 
mind you...) such  word was spanned among different parts of a CPU word, known in the
technical documentation of this dinosaur as "address register" and "decrement register
respectively. Thus the mnemonic names of "Content of the Address Register" and "Content
of Decrement Register" (aka "CAR" and "CDR") for these two pointers, names which were
used for the (lisp) functions allowing to access them.

Those names have stuck mainly for historical (sentimental ? hysterical ? ) reasons.
Even when "reasonable" synonyms were introduced (e. g. "first" and "rest" in Common Lisp),
all the old hands (and young dummies who wanted to emulate them) kept "car" and "cdr" close
to their hearts.

So, fifty one years after McCarthy stroke of genius, this piece of CS snobbery is still
with us (and probably will 'til 64-bit Linux time counters roll over...).

Despite its C-like syntax, its huge collection of array and array-like structures,
its loops, S and R are fundamentally Lisp-like languages. Most notably, the representation
of executable code is accessible by the code itself : one can create a function which
computes another function. Lisp was the first language explicitly created for this
purpose, and  it is no happenstance that it (or one of his almost uncountable dialects)
that many (most ?) of such language use Lisp fundamentals. Many of R/S "common way of
doing things" have a Lisp smell : for example, it is no chance that {s|t|l}apply(),
outer() and suchlike are *way* more efficient than for(), and they are strongly
reminescent of Lisp's mapcar...

BTW, this ability to "compute the language" is probably the most fundamental point
distinguishing S/R from all the rest of "statistical packages" (SPSS, Stata, SAS and
the rest of the crowd...

Now I have to say that I'm not old enough to have first-hand knowledge of this history.
I was born, but not weaned, when McCarthy unleashed Lisp on an unsuspecting world ...
I learned that ca. 1978-9, while discovering VLisp. 

While I can't really help you (I still think that processing "..." at C level is either
hubris of the worst sort, pure folly or a desperate case), I hope to have entertained you.

Sincerely,

					Emmanuel Charpentier



More information about the R-help mailing list