ZetCode

Scheme expressions and evaluation

last modified November 24, 2026 Guile 3.0

A Scheme program is built from expressions, and running the program means evaluating them. The rules are few: a number or a string evaluates to itself, a name evaluates to the value bound to it, and a list in parentheses is a procedure call, unless its first element is one of a small number of special keywords. This article goes through these rules with GNU Guile 3.0.7 and shows how quotation lets a program treat code as data.

Prefix notation

In a call, the operator is written first and the operands follow it, separated by spaces. Many arithmetic procedures accept any number of operands, which removes the need to repeat the operator as in 1 + 2 + 3.

prefix.scm
(display (+ 1 2 3 4 5))
(newline)
(display (* 2 3 4))
(newline)
(display (- 10 1 2))
(newline)
(display (- 7))
(newline)
(display (+))
(newline)
(display (*))
(newline)

+ adds all of its operands and * multiplies them. - subtracts every later operand from the first one, so (- 10 1 2) is 7, and with a single operand it negates it. With no operands at all, + returns 0 and * returns 1, the neutral elements of addition and multiplication.

$ guile prefix.scm
15
24
7
-7
0
1

Nesting

An operand may itself be a call, and the nesting can go as deep as needed. The parentheses show the structure of the computation exactly, so the reader never has to remember which operator binds more tightly. Long expressions are spread over several lines and indented so that the operands of a call line up.

nesting.scm
;; (2 + 3) * (10 - 4) / 5
(display (/ (* (+ 2 3) (- 10 4)) 5))
(newline)

;; the same expression spread over several lines
(display
 (/ (* (+ 2 3)
       (- 10 4))
    5))
(newline)

;; comparisons are procedures too
(display (< 1 2 3 4))
(newline)
(display (< 1 3 2))
(newline)

Both versions compute (2 + 3) × (10 − 4) / 5, which is 6; the line breaks and indentation make no difference to the result. Comparisons follow the same prefix form. (< 1 2 3 4) asks whether the operands are in increasing order and returns #t, while (< 1 3 2) returns #f.

$ guile nesting.scm
6
6
#t
#f

The evaluation rule

A call is evaluated in two steps. First, every element of the list is evaluated: the operator gives a procedure and each operand gives an argument value. Then the procedure is applied to the arguments. The next program makes the first step visible with a helper that prints a label and returns its second argument unchanged.

evaluation.scm
(define (trace label value)
  (display "evaluating ")
  (display label)
  (newline)
  value)

(define result
  ((trace "operator" +)
   (trace "first operand" 2)
   (trace "second operand" 3)))

(display "result: ")
(display result)
(newline)

Even the operator position is an ordinary expression: (trace "operator" +) returns the addition procedure, which is then applied to 2 and 3. Guile evaluated the elements from left to right in this run, but the Scheme standard leaves the order unspecified, and an implementation may choose another one. A program should therefore not depend on the order in which the operands of one call are evaluated.

$ guile evaluation.scm
evaluating operator
evaluating first operand
evaluating second operand
result: 5

Self-evaluating values

Numbers, strings, characters, and booleans are self-evaluating: the value of the expression 42 is the number 42 itself. They are the leaves of every computation. The program prints them with write, which shows each value in the form Scheme would read back.

selfeval.scm
(write 42)
(newline)
(write -3.5)
(newline)
(write "text")
(newline)
(write #\a)
(newline)
(write #t)
(newline)
(write (+ 40 2))
(newline)

The output matches the source text for each literal. The last expression is a call, but its value is the number 42, and it is printed in exactly the same way as the literal 42: once evaluated, a value does not remember where it came from.

$ guile selfeval.scm
42
-3.5
"text"
#\a
#t
42

Quote

Sometimes a program needs the expression itself rather than its value. The special form (quote x) returns x unevaluated, and the apostrophe is a shorthand for it: 'x means exactly (quote x). A quoted name is a symbol, and a quoted parenthesised expression is a list.

quoting.scm
(write (+ 1 2))
(newline)
(write '(+ 1 2))
(newline)
(write (quote (+ 1 2)))
(newline)
(write 'apple)
(newline)
(write ''apple)
(newline)
(write '42)
(newline)
(write '(1 (2 3) "four"))
(newline)

(+ 1 2) is evaluated and gives 3, while '(+ 1 2) gives a list of three elements: the symbol + and the numbers 1 and 2. 'apple is the symbol apple, and quoting it twice gives the list (quote apple), which shows what the apostrophe stands for. Quoting a self-evaluating value such as 42 changes nothing. Symbols and lists are described in the data types article.

$ guile quoting.scm
3
(+ 1 2)
(+ 1 2)
apple
(quote apple)
42
(1 (2 3) "four")

Special forms

A few keywords do not follow the evaluation rule. define, quote, and if are special forms: each has its own rule for which parts are evaluated and when. if, for instance, evaluates its test and then only one of the two branches. The difference becomes visible when the branches have side effects.

special.scm
(define (loud-yes)
  (display "computing yes")
  (newline)
  'yes)

(define (loud-no)
  (display "computing no")
  (newline)
  'no)

;; if evaluates the test and only one branch
(write (if (> 5 3) (loud-yes) (loud-no)))
(newline)

;; a procedure call evaluates every operand first
(define (choose test a b)
  (if test a b))

(write (choose (> 5 3) (loud-yes) (loud-no)))
(newline)

The first if evaluates only (loud-yes), so a single line is printed before the result. choose looks like if, but it is an ordinary procedure, and a procedure call evaluates all of its operands before the body runs: both loud-yes and loud-no print their message, even though only one result is used. This is why if cannot be written as a procedure and has to be part of the language. Macros, described later in the series, let a program define new special forms.

$ guile special.scm
computing yes
yes
computing yes
computing no
yes

Sequencing with begin

begin evaluates several expressions in order and returns the value of the last one. It is useful where the syntax allows only one expression but several actions are needed. A procedure body behaves like an implicit begin: its expressions run in order, and the last one gives the result.

sequence.scm
(define total
  (begin
    (display "step one")
    (newline)
    (display "step two")
    (newline)
    (+ 20 22)))

(display total)
(newline)

(define (report label value)
  (display label)
  (display ": ")
  (display value)
  (newline)
  value)

(display (* 2 (report "inner" 21)))
(newline)

The begin expression prints two lines and then evaluates to 42, which becomes the value of total. report prints a label and returns the value it received, so it can sit in the middle of an expression to show an intermediate result: (* 2 (report "inner" 21)) prints the inner value and still computes 42.

$ guile sequence.scm
step one
step two
42
inner: 21
42

Procedures are values

The name + is a variable like any other, and its value is a procedure. A procedure can be printed, bound to another name, or returned from an expression, and an expression in the operator position can compute which procedure to call.

values.scm
(define (double x) (* 2 x))

(display double)
(newline)
(display +)
(newline)
(display ((if (> 3 2) + -) 10 4))
(newline)
(display ((if (< 3 2) + -) 10 4))
(newline)
(define plus +)
(display (plus 1 2 3))
(newline)

Guile prints a procedure as #<procedure ...> with its name and parameter list; the built-in + takes optional and rest arguments. In ((if (> 3 2) + -) 10 4), the if expression selects the procedure +, which is applied to 10 and 4; the second call selects -. Binding plus to + copies the procedure value, not its name, so plus adds just like +.

$ guile values.scm
#<procedure double (x)>
#<procedure + (#:optional _ _ . _)>
14
6
6

Calling a value that is not a procedure

Because the first element of a list is always the operator, a habit carried over from infix languages leads to a run-time error. The next program writes (5 + 3), which asks Scheme to call the number 5 with the arguments + and 3.

notproc.scm
(display "calling a number")
(newline)
(display (5 + 3))
(newline)

The compiler accepts the program, because the error depends on a value only known when the program runs. The first line is printed, and then the evaluation stops with Wrong type to apply: 5, the Guile message for a call whose operator is not a procedure. As explained in the basics article, the backtrace is left out and the path is shortened to the file name.

$ guile notproc.scm
calling a number
notproc.scm:3:9: Wrong type to apply: 5

Code is data

A quoted expression is an ordinary list, and eval evaluates such a list as code. Its second argument is the environment in which the names are looked up; (interaction-environment) is the top-level environment of the program. A program can therefore build an expression as data and run it.

codedata.scm
(define expr '(* (+ 2 3) 4))

(write expr)
(newline)
(display (eval expr (interaction-environment)))
(newline)

(define same-expr (list '* (list '+ 2 3) 4))

(write same-expr)
(newline)
(display (eval same-expr (interaction-environment)))
(newline)

expr is the quoted list (* (+ 2 3) 4). write prints it as a list, and eval treats the same list as code and computes 20. same-expr builds an equal list with list, which collects its arguments into a list, and with quoted symbols for the operators. Programs and data share one notation, which is why Scheme programs can generate and transform code so easily. Most programs do not need eval, but it shows clearly that a Scheme expression is a list first and code second.

$ guile codedata.scm
(* (+ 2 3) 4)
20
(* (+ 2 3) 4)
20

Source

About expressions — GNU Guile Reference Manual

R7RS: the Revised7 Report on Scheme

In this article we have looked at prefix notation and nesting, the evaluation rule for calls, self-evaluating values, quotation, special forms, begin, procedures as values, and the use of eval on a quoted list. The data types article describes the values these expressions produce, and the variables and binding article covers the forms that introduce names.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Scheme tutorials.