Chapter 1. Introduction

Chez Scheme is an implementation of ANSI/IEEE Standard Scheme [21] with support for all required and optional features of the Revised5 Report on Scheme [7,22] along with numerous language and programming environment extensions.

This book describes these extensions in detail. It contains as well a description of the formal syntax of the extended language and a concise summary of standard and Chez Scheme forms and procedures, which gives the syntax of each form and the number and types of arguments accepted by each procedure. Details on standard Scheme features can be found in The Scheme Programming Language, Third Edition [10] or the Revised5 Report on Scheme. The Scheme Programming Language, Third Edition also contains an introduction to the Scheme language.

Most of this document also applies equally to Petite Chez Scheme, which is fully compatible with the complete Chez Scheme system but uses a high-speed interpreter in place of Chez Scheme's incremental native-code compiler. Programs written for Chez Scheme run unchanged in Petite Chez Scheme as long as they do not require the compiler to be invoked. In fact, Petite Chez Scheme is built from the same sources as Chez Scheme, with all but the compiler sources included.

The remainder of this chapter covers Chez Scheme extensions to Scheme syntax (Section 1.1), notational conventions used in this book (Section 1.2), the use of parameters for system customization (Section 1.3), and where to look for more information on Chez Scheme (Section 1.4).

Chapter 2 describes how one uses Chez Scheme for program development, scripting, and application delivery, plus how to get the compiler to generate the most efficient code possible. Chapter 3 describes debugging and object inspection facilities. Chapter 4 documents facilities for interacting with separate processes or code written in other languages. Chapter 5 describes binding forms. Chapter 6 documents control structures. Chapter 7 documents operations on nonnumeric objects, while Chapter 8 documents various numeric operations, including efficient type-specific operations. Chapter 9 describes input/output operations and generic ports, which allow the definition of ports with arbitrary input/output semantics. Chapter 10 describes syntactic extension and modules. Chapter 11 describes system operations, such as operations for interacting with the operating system and customizing Chez Scheme's user interface. Chapter 12 describes how to invoke and control the storage management system and documents guardians and weak pairs. Chapter 15 describes various compatibility features.

The back of this book contains a bibliography, the description of formal syntax, the summary of forms, and an index. The page numbers appearing in the summary of forms and the italicized page numbers appearing in the index indicate the locations in the text where forms and procedures are formally defined.

Acknowledgements: Mike Ashley, Carl Bruggeman, Bob Burger, Bob Hieb, and Oscar Waddell contributed significantly to the development of Chez Scheme and to this book. Many of the features documented in this book were suggested by current Chez Scheme users, and many of their comments also led to improvements in the text. Additional suggestions for improvements to Chez Scheme and to this book are welcome.

About the cover illustration: The illustration appearing on the cover was created by artist Jean-Pierre Hébert, who writes and uses Scheme programs as part of his creative process. More of his artwork can be found on his website,

Section 1.1. Chez Scheme Syntax

Chez Scheme extends Scheme's syntax both at the object (datum) level and at the level of syntactic forms. At the object level, Chez Scheme supports representations for symbols that contain nonstandard characters, IEEE floating-point infinities and NANs, nondecimal numbers expressed in floating-point and scientific notation, vectors with explicit lengths, shared and cyclic structures, records, boxes, and more.

Object-level extensions are summarized below and detailed in the formal syntax appendix in the back of this book. Form-level extensions are described throughout the book and summarized in the Summary of Forms, which also appears in the back of this book.

The standard syntax for identifiers (symbols, variables, keywords, and module names) requires that they be formed from the following set of characters:

Standard identifiers normally cannot start with any character that may start a number, i.e., a digit, plus sign ( + ), minus sign ( - ), or decimal point ( . ). Exceptions are +-, and ..., which are valid identifiers. For example, hi, Hello, n, x, x3, and ?$&*!!! are all identifiers. Identifiers must be delimited by whitespace, parentheses, a string (double) quote ( " ), or the comment character ( ; ).

Chez Scheme extends the syntax of identifiers in several ways. First, the sequence of characters making up an identifier's name may start with digits, periods, plus signs, and minus signs as long as the sequence cannot be parsed as a number. For example, 0abc, +++, and .. are all valid identifiers in Chez Scheme. Second, identifiers may contain embedded # characters, e.g., abc#def. Third, the single-character sequences { and } are identifiers. Finally, identifiers containing arbitrary characters may be printed by escaping them them with \ or with |. \ is used to escape a single character, the one that follows, as in the syntax of strings, whereas | is used to escape the group of characters that follow it up through the matching |. For example, \||\| is an identifier with a two-character name consisting of the character | followed by the the character \, and |hit me!| is an identifier whose name contains a space. Upper-case letters are usually converted to lower-case letters in an identifier's name, but escaping them prevents this conversion from taking place. For example, while ABC and Abc are both equivalent to abc, |ABC|, |Abc|, and abc are all distinct identifiers.

In addition, gensyms (page 7.7) are printed #{ and } brackets that enclose both the "pretty" and "unique" names, e.g., syntax that includes both "pretty" and unique names, e.g., #{g1426 |$RwJ@8KRjtz;\\$|}. They may also be printed using the pretty name only with the prefix #:, e.g., #:g1426.

Chez Scheme allows matched sets of square brackets, [ and ], to be used in place of parentheses. This feature is often used when typing programs to set off certain portions of the syntax. For example,

(let ([x e1] [y e2])
    [(eq? x y) 'yes]
    [else 'no]))

is equivalent to

(let ((x e1) (y e2))
    ((eq? x y) 'yes)
    (else 'no)))

Arbitrary radixes from two through 36 may be specified with the prefix #nr, where n is the radix. Case is not significant, so #nR may be used as well. Digit values from 10 through 35 are specified as either lower- or upper-case alphabetic characters, just as for hexadecimal numbers. For example, #36rZZ is 35 × 36 + 35, or 1295.

Chez Scheme also permits nondecimal numbers to be printed in floating-point or scientific notation. For example, #o1.4 is equivalent to 1.5, and #b1e10 is equivalent to 4.0. Digits take precedence over exponent specifiers, so that #x1e20 is simply the four-digit hexadecimal number equivalent to 7712.

IEEE floating-point (inexact) infinities and NANs are represented by the syntaxes +inf.0 for positive infinity, -inf.0 for negative infinity, and +nan.0 for NANs. -nan.0 may be used to refer to NANs as well, although the system prints all NANs as +nan.0. Case is not significant, so that, for example, +Inf.0 and +INF.0 are both equivalent to +inf.0.

In addition to #\space and #\newline, Chez Scheme recognizes #\backspace, #\linefeed (same as \#newline), #\nul, #\page, #\return, #\rubout, and #\tab, Of these, all but \#rubout and #\nul are considered whitespace characters. Characters may also be printed with an octal syntax consisting of the prefix #\ followed by a three octal-digit sequence. For example, #\000 is equivalent to #\nul.

Vectors may be printed with an explicit length prefix, and when the explicit length prefix is specified, duplicate trailing elements may be omitted. For example, #(a b c) may be printed as #3(a b c), and a vector of length 100 containing all zeros may be printed as #100(0). The printer generally prints the prefix and suppresses duplicate trailing elements unless the parameter print-vector-length is set to false.

Boxes are single-celled objects, like pairs without cdr cells. They are printed with a #& prefix, e.g., #&17 is a box containing the integer 17.

Records are printed with the syntax #[type-name field ...], where the symbol type-name is the name of the record type and field ... are the printed representations for the contents of the fields of the record.

Shared and cyclic structure may be printed using the graph mark and reference prefixes #n= and #n#. #n= is used to mark an item in the input, and #n# is used to refer to the item marked n. For example, '(#1=(a) . #1#) is a pair whose car and cdr contain the same list, and #0=(a . #0#) is a cyclic list, i.e., its cdr is itself.

A syntax form (see page 216) may be abbreviated in the same manner as a quote form, using the prefix #' in place of '. That is, #'datum is equivalent to (syntax datum). A \#primitive form (see page 262) may be abbreviated similarly with the #% prefix, e.g., #%car or #2%car.

Chez Scheme employs a single end-of-file object, which is printed #!eof. If the end-of-file object appears outside of any datum within a file being loaded, load will treat it as if it were a true end of file and stop loading at that point. Inserting #!eof into the middle of a file can thus be handy when tracking down a load-time error.

Broken pointers in weak pairs (see page 290) are represented by the broken weak pointer object, which is printed #!bwp.

For backwards compatibility with early revised reports on Scheme, #t may be printed as #!true, #f as #!false, and () as #!null.

Fast-loading format objects (see Section 9.10), including object code produced by the compiler, are printed with the prefix #@.

Comments may begin as usual with a semicolon and continue to the end of a line.

;;; the following procedure computes the sum of the
;;; squares of its arguments
(define sum-of-squares
  (lambda args
    (apply + (map (lambda (x) (* x x)) args))))

Chez Scheme allows two additional ways to insert comments. First, comments may blocked with properly nested #| and |# pairs.

#| the following procedure computes the sum of the #| here's a
   nested comment just for fun |# squares of its arguments |#
(define sum-of-squares
  (lambda args
    (apply + (map (lambda (x) (* x x)) args))))

Second, comments may begin with the prefix #;, in which case they end with the end of the next datum. This is useful for quickly commenting out sections of code while developing a program.

(define test1
  (lambda (#;arg) ; no argument for now
    #; ; the following definition doesn't work
    (define nan? (lambda (x) (= x nan?)))
    ; this definition does
    (define nan? (lambda (x) (not (= x x))))
    (list (nan? 0.0) (nan? (/ 0.0 0.0)))))
(test1) <graphic> (#f #t)

Section 1.2. Notational Conventions

This book follows essentially the same notational conventions as The Scheme Programming Language, Third Edition. These conventions are repeated below, with notes specific to Chez Scheme.

When the value produced by a procedure or syntactic form is said to be unspecified, the value may be any Scheme object. Chez Scheme usually returns a unique void object (see void) whenever the result is unspecified; avoid counting on this behavior, however, especially if your program may be ported to another Scheme implementation. Printing of the void object is suppressed by Chez Scheme's waiter (read-evaluate-print loop).

This book uses the phrase "an error will be signaled" to identify errors that the implementation always detects and signals. The phrase "it is an error" identifies a situation that violates some requirement of the language but that the implementation may not necessarily detect. Except at the highest optimization level, Chez Scheme detects and signals errors in both categories via the error handling facilities described in 11.1 whenever feasible.

Although most Scheme expressions may evaluate to multiple values, this book usually refers to the result of an expression as a single value. Multiple values are produced by the Scheme procedure values and may be received using the Scheme procedure call-with-values. Both procedures are documented in Section 5.7 of The Scheme Programming Language, Third Edition.

Scheme objects are displayed in a typewriter typeface just as they are to be typed at the keyboard. This includes syntactic keywords, variables, constant objects, Scheme expressions, and example programs. An italic typeface is used to set off syntax variables in the descriptions of syntactic forms and arguments in the descriptions of procedures. Italics are also used to set off technical terms the first time they appear. In general, names written in typewriter font are never capitalized (even at the beginning of a sentence). The same is true for syntax variables written in italics.

In the description of a syntactic form or procedure, a pattern shows the syntactic form or the application of the procedure. The syntax keyword or procedure name is given in typewriter font, as are parentheses. The remaining pieces of the syntax or arguments are shown in italics, using names that imply the types of the expressions or arguments expected by the syntactic form or procedure. Ellipses are used to specify zero or more occurrences of a subexpression or argument.

Section 1.3. Parameters

All Chez Scheme system customization is done via parameters. A parameter is a procedure that encapsulates a hidden state variable. When invoked without arguments, a parameter returns the value of the encapsulated variable. When invoked with one argument, the parameter changes the value of the variable to the value of its argument. A parameter may signal an error if its argument is not appropriate.

New parameters may be created and used by programs running in Chez Scheme. Parameters are used rather than global variables for program customization for two reasons: First, unintentional redefinition of a customization variable can cause unexpected problems, whereas unintentional redefinition of a parameter simply makes the parameter inaccessible. For example, a program that defines *print-level* for its own purposes in early releases of Chez Scheme would have unexpected effects on the printing of Scheme objects, whereas a program that defines print-level for its own purposes simply loses the ability to alter the printer's behavior. Of course, a program that invokes print-level by accident can still affect the system in unintended ways, but such an occurrence is less likely, and can only happen in an incorrect program.

Second, invalid values for parameters can be detected and rejected immediately when the "assignment" is made, rather than at the point where the first use occurs, when it is too late to recover and reinstate the old value. For example, an assignment of *print-level* to -1 would not have been caught until the first call to write or pretty-print, whereas an attempted assignment of -1 to the parameter print-level, i.e., (print-level -1), is signaled as an error immediately, before the change is actually made.

See Section 11.10 for more information on creating and manipulating parameters. Built-in system parameters are described in different sections throughout this book and are listed along with other syntactic forms and procedures in the Summary of Forms in the back of this book.

Section 1.4. More Information

The articles and technical reports listed below document various features of Chez Scheme and its implementation:

Links to abstracts and electronic versions of these publications are available at the url Additional resources, including the freely downloadable and redistributable Petite Chez Scheme, the free Scheme Widget Library and IDE, online versions of this book and of The Scheme Programming Language, Third Edition, and links to various Scheme resources are available at

R. Kent Dybvig / Chez Scheme Version 7 User's Guide
Copyright © 2005 R. Kent Dybvig
Revised July 2007 for Chez Scheme Version 7.4
Cadence Research Systems /
Cover illustration © 1998 Jean-Pierre Hébert
ISBN: 0-9667139-1-5
to order this book / about this book