21

What is the programming paradigm of R (R as in GNU S)?

I believe myself familiar with programming languages of different conceptual paradigms (have programmend in C++, Java, Prolog and some other languages) but although I already write my own small R scripts, I am not sure which paradigm R is supposed to represent.

  • 4
    Hey I appreciate all answers, and Stack Exchange is a wonderful site. But marking this as "not a real question" is a little bit unfair in my opinion. There are several other questions here on Stack Exchange following the pattern "What is the programming paradigm of [some programming language]", and also it's a question that can clearly be answered unambiguously. I don't understand why you flag this down, Wooble, Joris Meys, Dirk Eddelbuettel, Andrie and Chase. –  May 23 '11 at 15:14
  • If you strongly disagree with the closing of this question, you can flag it for moderator attention, or post a question about it on Meta. – Matt Ball May 23 '11 at 15:17
  • 2
    Reopening question since I tend to agree with the request that this is not really off-topic. At most it might be a question that might be migrated to programmers.stackexchange.com, but for now I'm just reopening it. – Lasse V. Karlsen May 23 '11 at 21:48

2 Answers2

24

R supports a mixture of object-oriented and functional programming paradigms.

On the functional side it:

  • has first class functions
  • has lazy evaluation of arguments
  • encourages pure, side-effect free functions

But

  • it does not implement tail call recursion
  • and it's easy to create non-pure functions

On the object oriented side:

  • it has three built in OO paradigms: S3 and S4, which are immutable and support generic function style OO, and reference classes (aka R5) which are mutable, and support the more common message-passing style OO.

  • S4 is heavily influenced by the OO-style of common lisp (CLOS) and dylan.

  • There are also a number of contributed packages that provide other types of OO: proto, mutatr, R.oo, OOP.

But

  • The built-in OO tools provide little in the way of syntactic sugar.
hadley
  • 102,019
  • 32
  • 183
  • 245
5

According to Wikipedia (emphasis added),

R supports procedural programming with functions and object-oriented programming with generic functions. A generic function acts differently depending on the type of arguments it is passed. In other words the generic function recognizes the type of object and selects (dispatches) the function (method) specific to that type of object. For example, R has a generic print() function that can print almost every type of object in R with a simple "print(objectname)" syntax.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • R is in fact most of all a functional programming language, although not in the pure lisp sense of the word (exactly like it is not procedural in the C-sense of the word, or OOP in the Java-sense of the word) – Joris Meys May 23 '11 at 15:05
  • Do you have any references to back that up? Does R avoid mutability and have first-class functions? – Matt Ball May 23 '11 at 15:09
  • Okay. Typical Wikipedia explanation. I long ago stopped even checking Wikipedia. I understand what generic functions are but I don't think that has something to do with the underlying paradigm, though; you could have generic functions in any language which has functions. Just because there are generic functions doesn't make it any more object-oriented. –  May 23 '11 at 15:11
  • @Matt Ball : See the answers on this question : http://stackoverflow.com/questions/4874867/learning-functional-programming-with-r . Wikipedia isn't always right... ;-) – Joris Meys May 23 '11 at 15:12
  • @gojira : to see the object-oriented paradigms in R, check the S3 and S4 classes, and the recent addition of reference (R5) classes. The whole Bioconductor project (www.bioconductor.org) is completely object-oriented, using S4 classes. But not in the Java-sense of the word. – Joris Meys May 23 '11 at 15:14
  • R5 is a misnomer, there is no such thing. – mdsumner May 23 '11 at 21:42