Scheme basics
last modified November 24, 2026 Guile 3.0
This article covers the parts of Scheme that every program uses: calling
procedures, printing values, writing comments, naming values with
define, declaring the first procedures, and making a simple
decision. The examples run with GNU Guile 3.0.7, as described in the
introduction. Each program is a single file with
the .scm extension and runs with guile file.scm.
Expressions in parentheses
Almost everything in a Scheme program is an expression in parentheses. The
first element inside the parentheses is the operator and the rest are the
operands, so the sum of 2 and 3 is written (+ 2 3). This
prefix notation is used for arithmetic just as it is for every other
procedure call.
(display (+ 2 3)) (newline) (display (* 4 5)) (newline) (display (- 10 4)) (newline) (display (+ 1 (* 2 3))) (newline)
Each display call prints the value of the expression inside it.
The last expression nests one call in another: (* 2 3) is
evaluated first and gives 6, and then (+ 1 6) gives 7. Because the
parentheses mark every call explicitly, Scheme needs no rules about operator
precedence. The expressions and evaluation
article looks at this in more detail.
$ guile arithmetic.scm 5 20 6 7
display and write
Scheme has two procedures for printing a value. display writes it
in a form meant for people, while write writes it in a form that
Scheme can read back: strings get their quotes and escapes, and characters get
their #\ prefix. Inside a string, \" stands for a
double quote and \n for a line break.
(display "a \"quoted\" word") (newline) (write "a \"quoted\" word") (newline) (display #\x) (newline) (write #\x) (newline) (display "first line\nsecond line") (newline)
display prints the string without the surrounding quotes and with
the escaped quotes turned into real ones. write prints the same
string as it would appear in source code. The character #\x is
printed as the letter x by display and as
#\x by write. The last call shows that
\n inside a string starts a new line; the explicit
newline call does the same between the other values.
$ guile writing.scm a "quoted" word "a \"quoted\" word" x #\x first line second line
Comments
A semicolon starts a comment that runs to the end of the line. By convention a
single semicolon follows code on the same line, two semicolons start a comment
on its own line, and three introduce a whole file or a major part of it.
Scheme also has block comments between #| and |#, and
the datum comment #;, which comments out exactly one
expression.
;;; comments.scm: the kinds of Scheme comments ;; A line comment starts with a semicolon. (display "Comments are ignored.") ; this one follows code (newline) #| A block comment may span several lines. |# #;(display "this expression is skipped") (display (+ 1 #;100 2)) (newline)
The block comment spans three lines and is skipped entirely. The datum comment
before the first display removes the whole call, however long it
is, which is handy for switching off an expression without counting
parentheses. In (+ 1 #;100 2) it removes only the number 100, so
the sum is 3.
$ guile comments.scm Comments are ignored. 3
Definitions
define gives a name to a value. The name comes first and the value
second, and once defined the name can be used wherever the value could appear.
Names in Scheme may contain letters, digits, and many symbols such as
-, ?, !, and >, so
total-price and list->string are ordinary
identifiers.
(define language "Scheme") (define year 1975) (define width 7) (define height 6) (display language) (display " appeared in ") (display year) (newline) (display "area: ") (display (* width height)) (newline)
The program defines a string and three numbers. A name is just another
expression: evaluating language gives the string
"Scheme", and (* width height) multiplies the values
bound to the two names. A variable has no declared type; the value it holds
carries its own type, which is the topic of the
data types article.
$ guile definitions.scm Scheme appeared in 1975 area: 42
Procedures
The same define creates a procedure when the name is written in
parentheses together with the parameters: (define (square x) ...)
defines a procedure square with one parameter x. The
expressions after the parameter list form the body. They run in order, and the
value of the last one is the value that the procedure returns.
(define (greet name) (display "Hello there, ") (display name) (display "!") (newline)) (define (square x) (* x x)) (greet "Lucia") (greet "Peter") (display (square 7)) (newline) (display (+ (square 3) (square 4))) (newline)
greet is called for its effect: it prints a greeting and its
return value is not used. square is called for its value, the
product of x with itself. There is no return
statement; the result of (* x x) is the result of the call. A
user-defined procedure is called exactly like a built-in one, so
(square 3) can appear as an operand of +.
$ guile procedures.scm Hello there, Lucia! Hello there, Peter! 49 25
Making a decision
The if expression has a test and two branches. When the test is
true, the first branch is evaluated; otherwise the second one is. The
comparison procedures <, >,
<=, >=, and = return the boolean
values #t (true) and #f (false).
(define (describe celsius)
(display celsius)
(if (>= celsius 24)
(display " C: hot")
(display " C: not hot"))
(newline))
(define (absolute n)
(if (< n 0)
(- n)
n))
(describe 18)
(describe 26)
(display (absolute -12))
(newline)
(display (absolute 5))
(newline)
describe uses if to choose which text to print.
absolute uses it for its value: the whole if
expression evaluates to either (- n), the negation of
n, or n itself, and that value is returned. Because
if is an expression rather than a statement, it can stand
anywhere a value is expected. The conditionals
article covers cond, case, when, and
unless.
$ guile decide.scm 18 C: not hot 26 C: hot 12 5
Reading an error report
When something goes wrong, Guile stops the program and prints a report. The next
program misspells the name radius in the expression that computes
the area.
(define radius 5) (display "area: ") (display (* 3.14159 radius raduis)) (newline)
The compiler already warns about the unknown name raduis, but the
program still runs: area: is printed, and then the evaluation of
the misspelled name fails. The report starts with a backtrace, the
chain of calls that led to the error, most of which lies inside Guile itself.
The useful part is the last line: the file, the line and column (4:27), and the
message Unbound variable: raduis. Guile prints the absolute path
of the file in the warning and in the report; it is shortened to the file name
here. Later articles show only the warning and the final line of such
reports.
$ guile unbound.scm
;;; unbound.scm:4:27: warning: possibly unbound variable `raduis'
area: Backtrace:
In ice-9/boot-9.scm:
1752:10 6 (with-exception-handler _ _ #:unwind? _ # _)
In unknown file:
5 (apply-smob/0 #<thunk 71cf25d40540>)
In ice-9/boot-9.scm:
724:2 4 (call-with-prompt _ _ #<procedure default-prompt-handle…>)
In ice-9/eval.scm:
619:8 3 (_ #(#(#<directory (guile-user) 71cf25d44c80>)))
In ice-9/boot-9.scm:
2835:4 2 (save-module-excursion _)
4380:12 1 (_)
In unbound.scm:
4:27 0 (_)
unbound.scm:4:27: Unbound variable: raduis
Source
Hello Scheme! — GNU Guile Reference Manual
R7RS: the Revised7 Report on Scheme
In this article we have written prefix expressions, printed values with
display and write, used the three kinds of comments,
named values and procedures with define, made a first decision
with if, and read an error report. The
expressions and evaluation article explains
the rules behind these programs, and the
variables and binding article covers
set! and local names.
Author
List all Scheme tutorials.