← back to programming

Expressions

Abelson & Sussman · 1996 · SICP §1.1

Programs are expressions. Evaluation = substitution. The environment maps names to values. Every compound expression is a tree of operator and operands, evaluated inside-out.

+ * - 3 5 10 6 (+ (* 3 5) (- 10 6)) = 19 — evaluated inside-out

Primitive expressions

The simplest expressions are self-evaluating: numbers evaluate to themselves, strings evaluate to themselves. The interpreter reads, evaluates, and prints. That loop is the whole interface.

Scheme

Combinations — prefix notation

A combination is a list where the first element is the operator and the rest are operands. Parentheses make the structure explicit. There is no precedence to memorize — nesting is the only grouping mechanism.

Scheme

Naming — define

define binds a name to a value in the environment. Once bound, the name can stand in for the value anywhere. This is the simplest form of abstraction: giving things names so we don't have to remember their values.

Scheme

Conditional expressions — cond and if

cond tests clauses in order and returns the value of the first true one. if is the two-branch special case. Both are special forms: they don't evaluate all their arguments.

Scheme

The substitution model

To evaluate a combination: evaluate the subexpressions, then apply the operator to the operand values. For compound procedures, substitute the argument values for the formal parameters in the body. This is the substitution model — simple, mechanical, and sufficient until we introduce assignment in Chapter 3.

Scheme

Notation reference

Scheme Python Meaning
(+ 1 2)1 + 2Combination (prefix vs infix)
(define x 5)x = 5Bind name to value
(define (f x) body)def f(x): bodyDefine a procedure
(cond (p1 e1) (p2 e2))if p1: e1; elif p2: e2Conditional
(if test then else)then if test else elseTwo-branch conditional
Neighbors

Other reading pages

Foundations (Wikipedia)

Translation notes

Scheme uses prefix notation uniformly; Python uses infix for arithmetic and keyword syntax for definitions and conditionals. The substitution model described here is applicative-order (evaluate arguments first). Python also uses applicative order. Normal-order (evaluate arguments only when needed) appears in Chapter 15 (Lazy Evaluation). The cond special form has no direct Python equivalent — the closest is an if/elif chain.

Ready for the real thing? Read SICP §1.1.