[Rd] C vs. C++ as learning and development tool for R
ross at biostat.ucsf.edu
Sat Jan 20 00:46:58 CET 2007
On Fri, Jan 19, 2007 at 03:55:30AM -0500, Kimpel, Mark William wrote:
> I have 3 years of experience with R and have an interest in becoming a
> better programmer so that I might someday be able to contribute
> packages. Other than R, my only experience was taking Lisp from Daniel
> Friedman in the 1970's. I would like to learn either C or C++ for
> several reasons:
> To gain a better concept of object oriented programming so that I can
> begin to use S4 methods in R.
> To perhaps speed up some things I do repeatedly in R
> To be able to contribute a package someday.
> I have been doing some reading and from what I can tell R is more
> compatible with C, but C++ has much greater capabilities for OO
> I have just started reading The C++ Programming Language: Special
> Edition by Bjarne Stroustrup
> ustrup&z=y> , he recommends first learning C++ and then then C if
> necessary, but as a developer of C++, he is probably biased.
> I would greatly appreciate the advice of the R developers and package
> contributors on this subject. "C" or "C++"?
To echo several other comments, if your goal is to work in R, it would
be best to go straight to R. I haven't used lisp much, but I believe
it is much closer to R than most other languages you could pick. It
has a functional style, and I recall reading the R's scoping rules
were directly inspired by Scheme, a Lisp variant. In fact, I didn't
feel I fully grasped them until I looked at Abelson and Sussman's
"Structure and Interpretation of Computer Languages" (which uses
The "functional" OO of R is significantly different from the
"class-based" OO found in most languages calling themselves object
oriented, including C++, Java, Python and smalltalk. Learning those
other languages to understand R could actually interfere with learning
If and when you need speed, you can program in any language that
supports Fortran or C interfaces, which is almost all of them.
If you're just doing general education....
I use C++ in R, and I have to say that programming in C++ is a
wretched experience. You have to make a major committment to learning
the language, which is a minefield of gotcha's, to use it in full OO
style. As others on this list and Stroustrup suggest, you can use it
and just incrementally add features over what you would do in C. It
can also be speedy and powerful (to run, not to program in!), which is
why I'm using it.
For pure OO, I think you can't beat smalltalk, which is freely
available at www.squeak.org (also there is a GNU and several
commerical versions). The language rules and syntax fit on one page.
The catch is that to use it you need to learn the environment and the
class library; these too are big tasks.
Objective-C is a much more lightweight C'ish OO than C++ (the author
moved smalltalk concepts into C). It's available as part of the GNU
compilers. Unlike smalltalk, you might use it if you cared about
performance, and it's the native language of Mac OS-X. It has a
relatively small learning curve.
Python and Java are other choices for OO, both significantly simpler
than C++. I find Python to be simple and elegant; it's also nifty for
scripting random tasks. Java's widely used on the web and in the
enterprise. Eiffel is also interesting.
I can't say much about "libraries already on other machines", but the
C runtime is probably the one you can count on being there the most.
Of course, another route would be to explore other functional
languages, a terrain I barely know: Haskell, ML, OCaml... In
particular, some of them have lazy evaluation of arguments, which R
also employs. And there are the functional/object languages like CLOS
(I think the O in OCaml is Object).
Anyway, this risks becoming a general language thread. My main point,
as someone who's been there, is don't use C++ unless you have a
compelling reason and a lot of time!
(Among the languages listed, the ones I've used extensively are C,
C++, Objective-C, Python, R, and smalltalk.)
More information about the R-devel