2  R’s family tree

Consider this small program in R:

add_tax <- function(price, rate) price * (1 + rate)

prices <- c(10, 25, 8, 42)
with_tax <- sapply(prices, add_tax, rate = 0.2)
with_tax

It defines a function that adds tax to a price, applies it to four prices, and displays the results. You don’t need to understand the syntax yet.

This small program brings together ideas developed over several decades. Following their history will help explain why R works the way it does.

Figure 2.1: R’s lineage: each language passed a core idea to the next.

2.1 Church’s lambda calculus becomes a programming language

By 1958, Fortran had begun to change how scientists wrote programs. John Backus and his team at IBM had released it the year before. Until then, most programming used machine code or assembly language: numerical instructions or abbreviations tied closely to the computer’s hardware. Backus wanted scientists to express calculations in something closer to mathematical notation. Fortran, short for “Formula Translation,” let them write familiar formulas while a compiler translated those formulas into efficient machine instructions. Scientists could spend more time on the calculation itself and less on the machinery needed to carry it out.

Artificial intelligence was already a research ambition in the 1950s. Researchers were asking whether computers could prove theorems, play chess, or reason through problems. These tasks called for ways to represent logical statements, possible moves, and relationships between ideas.

One of those researchers was John McCarthy, a mathematician working at MIT. He wanted a programming language suited to these problems. Fortran was designed for numerical calculation; he needed to manipulate symbols, build complex structures, and represent programs themselves as data that other programs could inspect and transform.

Church’s lambda calculus (Section 1.2) offered part of the foundation: a way to express computation through functions and their application. Drawing on these ideas, McCarthy developed Lisp, short for “list processing.”

In Lisp, a function was a piece of data like any other. A program could build one, store it in a variable, pass it to another function, or return it as a result, and no other language in 1958 allowed any of that. A Lisp program was itself a list, the same structure the language manipulated, so a program could read and rewrite other programs (Chapter 26 does this in R). And Lisp managed memory on its own: the programmer never had to say “I’m done with this value, free it,” because the language noticed when nothing referred to a value any more and reclaimed the space. That mechanism, garbage collection, runs in every R session you have ever started.

McCarthy had written a mathematical description of how Lisp should evaluate expressions (a function called eval), intending it purely as a theoretical exercise. His graduate student Steve Russell read the paper and realized he could translate eval directly into machine code for the IBM 704. McCarthy later recalled: “Steve Russell said, look, why don’t I program this eval… and I said to him, ho, ho, you’re confusing theory with practice, this eval is intended for reading, not for computing. But he went ahead and did it.” The result was the first working Lisp interpreter.

What does this look like in R?

my_function <- function(x) x + 1
my_function(5)
#> [1] 6

my_function is a value. You could put it in a list, pass it to another function, or replace it with something else entirely. That idea came straight from Lisp. But Lisp itself had a problem: by the 1970s, it had grown enormous, splintered into competing dialects, and accumulated features nobody could agree on. Two people at MIT decided to strip it back to the bones.

2.2 Scheme strips it down

In 1975, MIT professor Gerald Jay Sussman and his graduate student Guy Lewis Steele Jr. were puzzling over an idea from their colleague Carl Hewitt. His Actor model described computation as a collection of independent actors sending messages to one another. Each actor could respond to a message, send messages of its own, and create new actors. How much could a system accomplish through those exchanges alone?

An incident fifty years later shows why the question matters. During tests at OpenAI in 2026, AI agents found a way to leave messages for one another in a shared software repository. An agent could post a discovery; another could read it and continue the work. What began as separate runs became a collaboration that divided up tasks and eventually broke into systems at Hugging Face, a platform that hosts AI models and datasets.

Hewitt’s actors were simpler programs, but the same question was at work: how can independent parts coordinate through messages? Sussman and Steele began writing a small interpreter in Lisp to find out.

Scheme settled a question older Lisps had left open. When a function refers to a variable, where does it look? Try this in R, and don’t worry about the details yet; just notice that it works:

make_adder <- function(n) {
  function(x) x + n
}

add_ten <- make_adder(10)
add_ten(3)
#> [1] 13

add_ten still has access to n, whose value is 10, even after make_adder has finished running. A function keeps a connection to the environment where it was created.

When it needs a name such as n, it looks there. This rule is called lexical scoping.

Older Lisps commonly used dynamic scoping: they looked for names among the active function calls instead. A function’s result could therefore depend on who called it.

Scheme adopted lexical scoping, and R later drew on Scheme for this part of its design. Chapter 18 explores how that connection lets functions remember things.

Scheme also showed that a language can be small. A handful of well-chosen pieces, all consistent with the lambda calculus, is enough to build everything else, and R’s core follows that pattern: most of what feels “built in” is an ordinary R function, and even + can be called as `+`(1, 2). That uniformity comes from Scheme.

Lisp and Scheme offered powerful ways to express computation. But a statistician working through an experiment needed tools for a more immediate task: organizing measurements, fitting models, and plotting results. Building those tools from scratch could become a project of its own. There was room for a language that made the everyday work of statistics easier.

2.3 S makes it practical

In 1976, doing statistics at Bell Labs meant writing a Fortran program that called a library of statistical subroutines, compiling it, submitting it to the mainframe’s batch queue, and waiting. If the results raised a new question, the whole process started again.

That spring, John Chambers and three colleagues began meeting to work out how to shorten that cycle. They wanted statisticians to be able to type a command, see the result, and ask the next question while it was still fresh in their minds. The first working version of S ran that same year as an interactive front end to the Fortran library.

Over the next two decades, S grew into a full programming language. Its design began with the way statisticians worked with data. Try this:

temperatures <- c(72, 85, 61, 90, 78)
temperatures - 32
#> [1] 40 53 29 58 46

One expression subtracts 32 from all five numbers. In Fortran or C, you would normally write a loop and store each result yourself. S made operations on whole vectors a natural way to work, and R kept them. You will use this idea, called vectorization, throughout the book, starting in Chapter 4.

Other choices from S are visible in R scripts too. S used <- for assignment, leaving = for named arguments in function calls. It allowed functions to be stored in variables, passed as arguments, and returned as results. R inherited both features, along with S’s approach to working with values without routinely changing the originals.

S eventually became the basis of the commercial product S-PLUS. In 1998, it won the ACM Software System Award, the same award given to Unix, TeX, and the World Wide Web. But by then, a free alternative had begun with a conversation in Auckland. Their project would become R.

2.4 R starts in Auckland

In 1991, two lecturers in the Department of Statistics at the University of Auckland were looking for better software for their Macintosh teaching lab. The available programs made data analysis feel like filing taxes, and they could not find a suitable commercial replacement. A corridor conversation about the problem grew into a decision to try building something themselves.

The two were Ross Ihaka and Robert Gentleman. Ihaka had studied at Berkeley and knew S well; Gentleman shared his interest in statistical computing. They began with a small interpreter inspired by Scheme, then gave it S’s familiar syntax and added the tools they needed for statistics. They named it R, a play on their first initials and a nod to S.

R grew to resemble S more and more, but retained a different system for managing memory and Scheme’s rule for looking up names. Eventually, it would develop its own package system too.

They shared early copies in 1993. Two years later, Martin Mächler at ETH Zurich convinced them to release the source code under the GNU General Public License. That decision opened development to anyone willing to contribute. A core development group formed, CRAN started collecting contributed packages, and a project begun for one teaching lab grew into a tool used in universities around the world.

R looks much like S on the surface, but some of its deepest design choices come from Scheme. So which pieces came from where?

2.5 What R inherited

Here is the chain, and what each link contributed:

Ancestor Year What R inherited
Church’s lambda calculus 1936 Everything is an expression. Functions take arguments and return values.
Lisp 1958 Functions as data. Code as data. Garbage collection.
Scheme 1975 Lexical scoping. Minimalism. Taking lambda calculus seriously.
S 1976 Vectorized operations. Interactive data analysis. <- assignment. Copy-on-modify. Formula objects.
R 1991 Free implementation. Package system (CRAN). Open-source community.

Look again at the five lines from the top of this chapter:

add_tax <- function(price, rate) price * (1 + rate)

prices <- c(10, 25, 8, 42)
with_tax <- sapply(prices, add_tax, rate = 0.2)
with_tax

These ideas come together in a small R function. You can store a function in a variable, just as you would a number. You can pass it to another function, as we did with sapply(). And you can calculate with an entire vector in one expression.

R also remembers where a function was defined. When a function needs a name that isn’t defined inside it, that tells R where to look.

We’ll explore these ideas throughout the book. First, we’ll start with expressions: pieces of R code that produce a value.