Scheme

Summary

The report gives a defining description of the programming language Scheme. Scheme is a statically scoped and properly tail-recursive dialect of the Lisp programming language invented by Guy Lewis Steele Jr. and Gerald Jay Sussman. It was designed to have an exceptionally clear and simple semantics and few different ways to form expressions. A wide variety of programming paradigms, including imperative, functional, and message passing styles, find convenient expression in Scheme.

The introduction offers a brief history of the language and of the report.

The first three chapters present the fundamental ideas of the language and describe the notational conventions used for describing the language and for writing programs in the language.

section Expressions and section Program structure describe the syntax and semantics of expressions, programs, and definitions.

section Standard procedures describes Scheme's built-in procedures, which include all of the language's data manipulation and input/output primitives.

section Formal syntax and semantics provides a formal syntax for Scheme written in extended BNF, along with a formal denotational semantics. An example of the use of the language follows the formal syntax and semantics.

The appendix describes a macro facility that may be used to extend the syntax of Scheme.

The report concludes with a bibliography and an alphabetic index.

Introduction

History

Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major programming paradigms in use today.

Scheme was one of the first programming languages to incorporate first class procedures as in the lambda calculus, thereby proving the usefulness of static scope rules and block structure in a dynamically typed language. Scheme was the first major dialect of Lisp to distinguish procedures from lambda expressions and symbols, to use a single lexical environment for all variables, and to evaluate the operator position of a procedure call in the same way as an operand position. By relying entirely on procedure calls to express iteration, Scheme emphasized the fact that tail-recursive procedure calls are essentially goto's that pass arguments. Scheme was the first widely used programming language to embrace first class escape procedures, from which all previously known sequential control structures can be synthesized. More recently, building upon the design of generic arithmetic in Common Lisp, Scheme introduced the concept of exact and inexact numbers. With the appendix to this report Scheme becomes the first programming language to support hygienic macros, which permit the syntax of a block-structured language to be extended reliably.

Background

The first description of Scheme was written in 1975 [SCHEME75]. A revised report [SCHEME78] appeared in 1978, which described the evolution of the language as its MIT implementation was upgraded to support an innovative compiler [RABBIT]. Three distinct projects began in 1981 and 1982 to use variants of Scheme for courses at MIT, Yale, and Indiana University [REES82] [MITSCHEME] [SCHEME311]. An introductory computer science textbook using Scheme was published in 1984 [SICP].

As Scheme became more widespread, local dialects began to diverge until students and researchers occasionally found it difficult to understand code written at other sites. Fifteen representatives of the major implementations of Scheme therefore met in October 1984 to work toward a better and more widely accepted standard for Scheme.

Their report [RRRS] was published at MIT and Indiana University in the summer of 1985. Another round of revision took place in the spring of 1986 [R3RS]. The present report reflects further revisions agreed upon in a meeting that preceded the 1988 ACM Conference on Lisp and Functional Programming and in subsequent electronic mail.

We intend this report to belong to the entire Scheme community, and so we grant permission to copy it in whole or in part without fee. In particular, we encourage implementors of Scheme to use this report as a starting point for manuals and other documentation, modifying it as necessary.

Acknowledgements

We would like to thank the following people for their help: Alan Bawden, Michael Blair, George Carrette, Andy Cromarty, Pavel Curtis, Jeff Dalton, Olivier Danvy, Ken Dickey, Andy Freeman, Richard Gabriel, Yekta G\"ursel, Ken Haase, Robert Hieb, Paul Hudak, Richard Kelsey, Morry Katz, Chris Lindblad, Mark Meyer, Jim Miller, Jim Philbin, John Ramsdell, Mike Shaff, Jonathan Shapiro, Julie Sussman, Perry Wagle, Daniel Weise, Henry Wu, and Ozan Yigit. We thank Carol Fessenden, Daniel Friedman, and Christopher Haynes for permission to use text from the Scheme 311 version 4 reference manual. We thank Texas Instruments, Inc. for permission to use text from the TI Scheme Language Reference Manual. We gladly acknowledge the influence of manuals for MIT Scheme, T, Scheme 84, Common Lisp, and Algol 60.

We also thank Betty Dexter for the extreme effort she put into setting this report in TeX, and Donald Knuth for designing the program that caused her troubles.

The Artificial Intelligence Laboratory of the Massachusetts Institute of Technology, the Computer Science Department of Indiana University, and the Computer and Information Sciences Department of the University of Oregon supported the preparation of this report. Support for the MIT work was provided in part by the Advanced Research Projects Agency of the Department of Defense under Office of Naval Research contract N00014-80-C-0505. Support for the Indiana University work was provided by NSF grants NCS 83-04567 and NCS 83-03325.

Overview of Scheme

Semantics

This section gives an overview of Scheme's semantics. A detailed informal semantics is the subject of section Basic concepts through section Standard procedures. For reference purposes, section Formal semantics provides a formal semantics of Scheme.

Following Algol, Scheme is a statically scoped programming language. Each use of a variable is associated with a lexically apparent binding of that variable.

Scheme has latent as opposed to manifest types. Types are associated with values (also called objects) rather than with variables. (Some authors refer to languages with latent types as weakly typed or dynamically typed languages.) Other languages with latent types are APL, Snobol, and other dialects of Lisp. Languages with manifest types (sometimes referred to as strongly typed or statically typed languages) include Algol 60, Pascal, and C.

All objects created in the course of a Scheme computation, including procedures and continuations, have unlimited extent. No Scheme object is ever destroyed. The reason that implementations of Scheme do not (usually!) run out of storage is that they are permitted to reclaim the storage occupied by an object if they can prove that the object cannot possibly matter to any future computation. Other languages in which most objects have unlimited extent include APL and other Lisp dialects.

Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure. Thus with a tail-recursive implementation, iteration can be expressed using the ordinary procedure-call mechanics, so that special iteration constructs are useful only as syntactic sugar.

Scheme procedures are objects in their own right. Procedures can be created dynamically, stored in data structures, returned as results of procedures, and so on. Other languages with these properties include Common Lisp and ML.

One distinguishing feature of Scheme is that continuations, which in most other languages only operate behind the scenes, also have "first-class" status. Continuations are useful for implementing a wide variety of advanced control constructs, including non-local exits, backtracking, and coroutines. See section Control features.

Arguments to Scheme procedures are always passed by value, which means that the actual argument expressions are evaluated before the procedure gains control, whether the procedure needs the result of the evaluation or not. ML, C, and APL are three other languages that always pass arguments by value. This is distinct from the lazy-evaluation semantics of Haskell, or the call-by-name semantics of Algol 60, where an argument expression is not evaluated unless its value is needed by the procedure.

Scheme's model of arithmetic is designed to remain as independent as possible of the particular ways in which numbers are represented within a computer. In Scheme, every integer is a rational number, every rational is a real, and every real is a complex number. Thus the distinction between integer and real arithmetic, so important to many programming languages, does not appear in Scheme. In its place is a distinction between exact arithmetic, which corresponds to the mathematical ideal, and inexact arithmetic on approximations. As in Common Lisp, exact arithmetic is not limited to integers.

Syntax

Scheme, like most dialects of Lisp, employs a fully parenthesized prefix notation for programs and (other) data; the grammar of Scheme generates a sublanguage of the language used for data. An important consequence of this simple, uniform representation is the susceptibility of Scheme programs and data to uniform treatment by other Scheme programs.

The read procedure performs syntactic as well as lexical decomposition of the data it reads. The read procedure parses its input as data (section External representations), not as program.

The formal syntax of Scheme is described in section Formal syntax.

Notation and terminology

Essential and non-essential features

It is required that every implementation of Scheme support features that are marked as being essential. Features not explicitly marked as essential are not essential. Implementations are free to omit non-essential features of Scheme or to add extensions, provided the extensions are not in conflict with the language reported here. In particular, implementations must support portable code by providing a syntactic mode that preempts no lexical conventions of this report and reserves no identifiers other than those listed as syntactic keywords in section Identifiers.

Error situations and unspecified behavior

When speaking of an error situation, this report uses the phrase "an error is signalled" to indicate that implementations must detect and report the error. If such wording does not appear in the discussion of an error, then implementations are not required to detect or report the error, though they are encouraged to do so. An error situation that implementations are not required to detect is usually referred to simply as "an error."

For example, it is an error for a procedure to be passed an argument that the procedure is not explicitly specified to handle, even though such domain errors are seldom mentioned in this report. Implementations may extend a procedure's domain of definition to include such arguments.

This report uses the phrase "may report a violation of an implementation restriction" to indicate circumstances under which an implementation is permitted to report that it is unable to continue execution of a correct program because of some restriction imposed by the implementation. Implementation restrictions are of course discouraged, but implementations are encouraged to report violations of implementation restrictions.

For example, an implementation may report a violation of an implementation restriction if it does not have enough storage to run a program.

If the value of an expression is said to be "unspecified," then the expression must evaluate to some object without signalling an error, but the value depends on the implementation; this report explicitly does not say what value should be returned.

Entry format

section Expressions and section Standard procedures are organized into entries. Each entry describes one language feature or a group of related features, where a feature is either a syntactic construct or a built-in procedure. An entry begins with one or more header lines of the form

{essential: category} template

if the feature is an essential feature, or simply

category: template

if the feature is not an essential feature.

If category is "syntax", the entry describes an expression type, and the header line gives the syntax of the expression type. Components of expressions are designated by syntactic variables, which are written using angle brackets, for example, <expression>, <variable>. Syntactic variables should be understood to denote segments of program text; for example, <expression> stands for any string of characters which is a syntactically valid expression. The notation

<thing 1> ...

indicates zero or more occurrences of a <thing>, and

<thing 1> <thing 2> ...

indicates one or more occurrences of a <thing>.

If category is "procedure", then the entry describes a procedure, and the header line gives a template for a call to the procedure. Argument names in the template are italicized. Thus the header line

essential procedure: vector-ref vector k

indicates that the essential built-in procedure vector-ref takes two arguments, a vector vector and an exact non-negative integer k (see below). The header lines

essential procedure: make-vector k

procedure: make-vector k fill

indicate that in all implementations, the make-vector procedure must be defined to take one argument, and some implementations will extend it to take two arguments.

It is an error for an operation to be presented with an argument that it is not specified to handle. For succinctness, we follow the convention that if an argument name is also the name of a type listed in section Disjointness of types, then that argument must be of the named type. For example, the header line for vector-ref given above dictates that the first argument to vector-ref must be a vector. The following naming conventions also imply type restrictions:

Evaluation examples

The symbol "=>" used in program examples should be read "evaluates to." For example,

(* 5 8)                     =>  40

means that the expression (* 5 8) evaluates to the object 40. Or, more precisely: the expression given by the sequence of characters "(* 5 8)" evaluates, in the initial environment, to an object that may be represented externally by the sequence of characters "40". See section External representations for a discussion of external representations of objects.

Naming conventions

By convention, the names of procedures that always return a boolean value usually end in "`?'". Such procedures are called predicates.

By convention, the names of procedures that store values into previously allocated locations (see section Storage model) usually end in "`!'". Such procedures are called mutation procedures. By convention, the value returned by a mutation procedure is unspecified.

By convention, "`->'" appears within the names of procedures that take an object of one type and return an analogous object of another type. For example, list->vector takes a list and returns a vector whose elements are the same as those of the list.

Lexical conventions

This section gives an informal account of some of the lexical conventions used in writing Scheme programs. For a formal syntax of Scheme, see section Formal syntax.

Upper and lower case forms of a letter are never distinguished except within character and string constants. For example, Foo is the same identifier as FOO, and #x1AB is the same number as #X1ab.

Identifiers

Most identifiers allowed by other programming languages are also acceptable to Scheme. The precise rules for forming identifiers vary among implementations of Scheme, but in all implementations a sequence of letters, digits, and "extended alphabetic characters" that begins with a character that cannot begin a number is an identifier. In addition, +, -, and ... are identifiers. Here are some examples of identifiers:

lambda                   q
list->vector             soup
+                        V17a
<=?                      a34kTMNs
the-word-recursion-has-many-meanings

Extended alphabetic characters may be used within identifiers as if they were letters. The following are extended alphabetic characters:

+ - . * / < = > ! ? : $ % _ & ~ ^

See section Lexical structure for a formal syntax of identifiers.

Identifiers have several uses within Scheme programs:

The following identifiers are syntactic keywords, and should not be used as variables:

=>           do            or
and          else          quasiquote
begin        if            quote
case         lambda        set!
cond         let           unquote
define       let*          unquote-splicing
delay        letrec

Some implementations allow all identifiers, including syntactic keywords, to be used as variables. This is a compatible extension to the language, but ambiguities in the language result when the restriction is relaxed, and the ways in which these ambiguities are resolved vary between implementations.

Whitespace and comments

Whitespace characters are spaces and newlines. (Implementations typically provide additional whitespace characters such as tab or page break.) Whitespace is used for improved readability and as necessary to separate tokens from each other, a token being an indivisible lexical unit such as an identifier or number, but is otherwise insignificant. Whitespace may occur between any two tokens, but not within a token. Whitespace may also occur inside a string, where it is significant.

A semicolon (;) indicates the start of a comment. The comment continues to the end of the line on which the semicolon appears. Comments are invisible to Scheme, but the end of the line is visible as whitespace. This prevents a comment from appearing in the middle of an identifier or number.

;;; The FACT procedure computes the factorial
;;; of a non-negative integer.
(define fact
  (lambda (n)
    (if (= n 0)
        1        ;Base case: return 1
        (* n (fact (- n 1))))))

Other notations

For a description of the notations used for numbers, see section Numbers.

. + -
These are used in numbers, and may also occur anywhere in an identifier except as the first character. A delimited plus or minus sign by itself is also an identifier. A delimited period (not occurring within a number or identifier) is used in the notation for pairs (section Pairs and lists), and to indicate a rest-parameter in a formal parameter list (section Lambda expressions). A delimited sequence of three successive periods is also an identifier.
( )
Parentheses are used for grouping and to notate lists (section Pairs and lists).
'
The single quote character is used to indicate literal data (section Literal expressions).
`
The backquote character is used to indicate almost-constant data (section Quasiquotation).
, ,@
The character comma and the sequence comma at-sign are used in conjunction with backquote (section Quasiquotation).
"
The double quote character is used to delimit strings (section Strings).
\
Backslash is used in the syntax for character constants (section Characters) and as an escape character within string constants (section Strings).
[ ] { }
Left and right square brackets and curly braces are reserved for possible future extensions to the language.
#
Sharp sign is used for a variety of purposes depending on the character that immediately follows it:
#t #f
These are the boolean constants (section Booleans).
#\
This introduces a character constant (section Characters).
#(
This introduces a vector constant (section Vectors). Vector constants are terminated by `)' .
#e #i #b #o #d #x
These are used in the notation for numbers (section Syntax of numerical constants).

Basic concepts

Variables and regions

Any identifier that is not a syntactic keyword (see section Identifiers) may be used as a variable. A variable may name a location where a value can be stored. A variable that does so is said to be bound to the location. The set of all visible bindings in effect at some point in a program is known as the environment in effect at that point. The value stored in the location to which a variable is bound is called the variable's value. By abuse of terminology, the variable is sometimes said to name the value or to be bound to the value. This is not quite accurate, but confusion rarely results from this practice.

Certain expression types are used to create new locations and to bind variables to those locations. The most fundamental of these binding constructs is the lambda expression, because all other binding constructs can be explained in terms of lambda expressions. The other binding constructs are let, let*, letrec, and do expressions (see section Lambda expressions, section Binding constructs, and section Iteration).

Like Algol and Pascal, and unlike most other dialects of Lisp except for Common Lisp, Scheme is a statically scoped language with block structure. To each place where a variable is bound in a program there corresponds a region of the program text within which the binding is effective. The region is determined by the particular binding construct that establishes the binding; if the binding is established by a lambda expression, for example, then its region is the entire lambda expression. Every reference to or assignment of a variable refers to the binding of the variable that established the innermost of the regions containing the use. If there is no binding of the variable whose region contains the use, then the use refers to the binding for the variable in the top level environment, if any (section section Standard procedures); if there is no binding for the identifier, it is said to be unbound.

True and false

Any Scheme value can be used as a boolean value for the purpose of a conditional test. As explained in section Booleans, all values count as true in such a test except for #f. This report uses the word "true" to refer to any Scheme value that counts as true, and the word "false" to refer to #f.

Note: In some implementations the empty list also counts as false instead of true.

External representations

An important concept in Scheme (and Lisp) is that of the external representation of an object as a sequence of characters. For example, an external representation of the integer 28 is the sequence of characters "28", and an external representation of a list consisting of the integers 8 and 13 is the sequence of characters "(8 13)".

The external representation of an object is not necessarily unique. The integer 28 also has representations "#e28.000" and "#x1c", and the list in the previous paragraph also has the representations "( 08 13 )" and "(8 . (13 . ()))" (see section Pairs and lists).

Many objects have standard external representations, but some, such as procedures, do not have standard representations (although particular implementations may define representations for them).

An external representation may be written in a program to obtain the corresponding object (see quote, section Literal expressions).

External representations can also be used for input and output. The procedure read (section Input) parses external representations, and the procedure Output (section Output) generates them. Together, they provide an elegant and powerful input/output facility.

Note that the sequence of characters "(+ 2 6)" is not an external representation of the integer 8, even though it is an expression evaluating to the integer 8; rather, it is an external representation of a three-element list, the elements of which are the symbol + and the integers 2 and 6. Scheme's syntax has the property that any sequence of characters that is an expression is also the external representation of some object. This can lead to confusion, since it may not be obvious out of context whether a given sequence of characters is intended to denote data or program, but it is also a source of power, since it facilitates writing programs such as interpreters and compilers that treat programs as data (or vice versa).

The syntax of external representations of various kinds of objects accompanies the description of the primitives for manipulating the objects in the appropriate sections of section Standard procedures.

Disjointness of types

No object satisfies more than one of the following predicates:

boolean?          pair?
symbol?           number?
char?             string?
vector?           procedure?

These predicates define the types boolean, pair, symbol, number, char (or character), string, vector, and procedure.

Storage model

Variables and objects such as pairs, vectors, and strings implicitly denote locations or sequences of locations. A string, for example, denotes as many locations as there are characters in the string. (These locations need not correspond to a full machine word.) A new value may be stored into one of these locations using the string-set! procedure, but the string continues to denote the same locations as before.

An object fetched from a location, by a variable reference or by a procedure such as car, vector-ref, or string-ref, is equivalent in the sense of eqv? (section section Equivalence predicates) to the object last stored in the location before the fetch.

Every location is marked to show whether it is in use. No variable or object ever refers to a location that is not in use. Whenever this report speaks of storage being allocated for a variable or object, what is meant is that an appropriate number of locations are chosen from the set of locations that are not in use, and the chosen locations are marked to indicate that they are now in use before the variable or object is made to denote them.

In many systems it is desirable for constants (i.e. the values of literal expressions) to reside in read-only-memory. To express this, it is convenient to imagine that every object that denotes locations is associated with a flag telling whether that object is mutable or immutable. The constants and the strings returned by symbol->string are then the immutable objects, while all objects created by the other procedures listed in this report are mutable. It is an error to attempt to store a new value into a location that is denoted by an immutable object.

Expressions

A Scheme expression is a construct that returns a value, such as a variable reference, literal, procedure call, or conditional.

Expression types are categorized as primitive or derived. Primitive expression types include variables and procedure calls. Derived expression types are not semantically primitive, but can instead be explained in terms of the primitive constructs as in section derived expression types. They are redundant in the strict sense of the word, but they capture common patterns of usage, and are therefore provided as convenient abbreviations.

Primitive expression types

Variable references

essential syntax: <variable>

An expression consisting of a variable

(section Variables and regions) is a variable reference. The value of the variable reference is the value stored in the location to which the variable is bound. It is an error to reference an unbound variable.

(define x 28)
x                           =>  28

Literal expressions

essential syntax: quote <datum>

essential syntax: '<datum>

essential syntax: <constant>

(quote <datum>) evaluates to <datum>. <Datum> may be any external representation of a Scheme object (see section External representations). This notation is used to include literal constants in Scheme code.

(quote a)                   =>  a
(quote #(a b c))            =>  #(a b c)
(quote (+ 1 2))             =>  (+ 1 2)

(quote <datum>) may be abbreviated as '<datum>. The two notations are equivalent in all respects.

'a                          =>  a
'#(a b c)                   =>  #(a b c)
'()                         =>  ()
'(+ 1 2)                    =>  (+ 1 2)
'(quote a)                  =>  (quote a)
''a                         =>  (quote a)

Numerical constants, string constants, character constants, and boolean constants evaluate "to themselves"; they need not be quoted.

'"abc"                      =>  "abc"
"abc"                       =>  "abc"
'145932                     =>  145932
145932                      =>  145932
'#t                         =>  #t
#t                          =>  #t

As noted in section Storage model, it is an error to alter a constant (i.e. the value of a literal expression) using a mutation procedure like set-car! or string-set!.

Procedure calls

essential syntax: <operator> <operand 1> ...

A procedure call is written by simply enclosing in parentheses expressions for the procedure to be called and the arguments to be passed to it. The operator and operand expressions are evaluated (in an unspecified order) and the resulting procedure is passed the resulting arguments.

(+ 3 4)                     =>  7
((if #f + *) 3 4)           =>  12

A number of procedures are available as the values of variables in the initial environment; for example, the addition and multiplication procedures in the above examples are the values of the variables + and *. New procedures are created by evaluating lambda expressions (see section section Lambda expressions).

Procedure calls are also called combinations.

Note: In contrast to other dialects of Lisp, the order of evaluation is unspecified, and the operator expression and the operand expressions are always evaluated with the same evaluation rules.

Note: Although the order of evaluation is otherwise unspecified, the effect of any concurrent evaluation of the operator and operand expressions is constrained to be consistent with some sequential order of evaluation. The order of evaluation may be chosen differently for each procedure call.

Note: In many dialects of Lisp, the empty combination, (), is a legitimate expression. In Scheme, combinations must have at least one subexpression, so () is not a syntactically valid expression.

Lambda expressions

essential syntax: lambda <formals> <body>

Syntax: <Formals> should be a formal arguments list as described below, and <body> should be a sequence of one or more expressions.

Semantics: A lambda expression evaluates to a procedure. The environment in effect when the lambda expression was evaluated is remembered as part of the procedure. When the procedure is later called with some actual arguments, the environment in which the lambda expression was evaluated will be extended by binding the variables in the formal argument list to fresh locations, the corresponding actual argument values will be stored in those locations, and the expressions in the body of the lambda expression will be evaluated sequentially in the extended environment. The result of the last expression in the body will be returned as the result of the procedure call.

(lambda (x) (+ x x))        =>  a procedure
((lambda (x) (+ x x)) 4)    =>  8

(define reverse-subtract
  (lambda (x y) (- y x)))
(reverse-subtract 7 10)     =>  3

(define add4
  (let ((x 4))
    (lambda (y) (+ x y))))
(add4 6)                    =>  10

<Formals> should have one of the following forms:

It is an error for a <variable> to appear more than once in <formals>.

((lambda x x) 3 4 5 6)      =>  (3 4 5 6)
((lambda (x y . z) z)
 3 4 5 6)                   =>  (5 6)

Each procedure created as the result of evaluating a lambda expression is tagged with a storage location, in order to make eqv? and eq? work on procedures (see section Equivalence predicates).

Conditionals

essential syntax: if <test> <consequent> <alternate>

syntax: if <test> <consequent>

Syntax: <Test>, <consequent>, and <alternate> may be arbitrary expressions.

Semantics: An if expression is evaluated as follows: first, <test> is evaluated. If it yields a true value (see section Booleans), then <consequent> is evaluated and its value is returned. Otherwise <alternate> is evaluated and its value is returned. If <test> yields a false value and no <alternate> is specified, then the result of the expression is unspecified.

(if (> 3 2) 'yes 'no)       =>  yes
(if (> 2 3) 'yes 'no)       =>  no
(if (> 3 2)
    (- 3 2)
    (+ 3 2))                =>  1

Assignments

essential syntax: set! <variable> <expression>

<Expression> is evaluated, and the resulting value is stored in the location to which <variable> is bound. <Variable> must be bound either in some region enclosing the set! expression or at top level. The result of the set! expression is unspecified.

(define x 2)
(+ x 1)                     =>  3
(set! x 4)                  =>  unspecified
(+ x 1)                     =>  5

Derived expression types

For reference purposes, section derived expression types gives rewrite rules that will convert constructs described in this section into the primitive constructs described in the previous section.

Conditionals

essential syntax: cond <clause 1> <clause 2> ...

Syntax: Each <clause> should be of the form

(<test> <expression> ...)

where <test> is any expression. The last <clause> may be an "else clause," which has the form

(else <expression 1> <expression 2> ...).

Semantics: A cond expression is evaluated by evaluating the <test> expressions of successive <clause>s in order until one of them evaluates to a true value (see section Booleans). When a <test> evaluates to a true value, then the remaining <expression>s in its <clause> are evaluated in order, and the result of the last <expression> in the <clause> is returned as the result of the entire cond expression. If the selected <clause> contains only the <test> and no <expression>s, then the value of the <test> is returned as the result. If all <test>s evaluate to false values, and there is no else clause, then the result of the conditional expression is unspecified; if there is an else clause, then its <expression>s are evaluated, and the value of the last one is returned.

(cond ((> 3 2) 'greater)
      ((< 3 2) 'less))      =>  greater

(cond ((> 3 3) 'greater)
      ((< 3 3) 'less)
      (else 'equal))        =>  equal

Some implementations support an alternative <clause> syntax, (<test> => <recipient>), where <recipient> is an expression. If <test> evaluates to a true value, then <recipient> is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the <test>.

(cond ((assv 'b '((a 1) (b 2))) => cadr)
      (else #f))     =>  2

essential syntax: case <key> <clause 1> <clause 2> ...

Syntax: <Key> may be any expression. Each <clause> should have the form

((<datum 1> ...) <expression 1> <expression 2> ...),

where each <datum> is an external representation of some object. All the <datum>s must be distinct. The last <clause> may be an "else clause," which has the form

(else <expression 1> <expression 2> ...).

Semantics: A case expression is evaluated as follows. <Key> is evaluated and its result is compared against each <datum>. If the result of evaluating <key> is equivalent (in the sense of eqv?; see section Equivalence predicates) to a <datum>, then the expressions in the corresponding <clause> are evaluated from left to right and the result of the last expression in the <clause> is returned as the result of the case expression. If the result of evaluating <key> is different from every <datum>, then if there is an else clause its expressions are evaluated and the result of the last is the result of the case expression; otherwise the result of the case expression is unspecified.

(case (* 2 3)
  ((2 3 5 7) 'prime)
  ((1 4 6 8 9) 'composite)) =>  composite
(case (car '(c d))
  ((a) 'a)
  ((b) 'b))                 =>  unspecified
(case (car '(c d))
  ((a e i o u) 'vowel)
  ((w y) 'semivowel)
  (else 'consonant))        =>  consonant

essential syntax: and <test 1> ...

The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see section Booleans) is returned. Any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then #t is returned.

(and (= 2 2) (> 2 1))       =>  #t
(and (= 2 2) (< 2 1))       =>  #f
(and 1 2 'c '(f g))         =>  (f g)
(and)                       =>  #t

essential syntax: or <test 1> ...

The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see section Booleans) is returned. Any remaining expressions are not evaluated. If all expressions evaluate to false values, the value of the last expression is returned. If there are no expressions then #f is returned.

(or (= 2 2) (> 2 1))        =>  #t
(or (= 2 2) (< 2 1))        =>  #t
(or #f #f #f)               =>  #f
(or (memq 'b '(a b c))
    (/ 3 0))                =>  (b c)

Binding constructs

The three binding constructs let, let*, and letrec give Scheme a block structure, like Algol 60. The syntax of the three constructs is identical, but they differ in the regions they establish for their variable bindings. In a let expression, the initial values are computed before any of the variables become bound; in a let* expression, the bindings and evaluations are performed sequentially; while in a letrec expression, all the bindings are in effect while their initial values are being computed, thus allowing mutually recursive definitions.

essential syntax: let <bindings> <body>

Syntax: <Bindings> should have the form

((<variable 1> <init 1>) ...),

where each <init> is an expression, and <body> should be a sequence of one or more expressions. It is an error for a <variable> to appear more than once in the list of variables being bound.

Semantics: The <init>s are evaluated in the current environment (in some unspecified order), the <variable>s are bound to fresh locations holding the results, the <body> is evaluated in the extended environment, and the value of the last expression of <body> is returned. Each binding of a <variable> has <body> as its region.

(let ((x 2) (y 3))
  (* x y))                  =>  6

(let ((x 2) (y 3))
  (let ((x 7)
        (z (+ x y)))
    (* z x)))               =>  35

See also named let, section Iteration.

syntax: let* <bindings> <body>

Syntax: <Bindings> should have the form

((<variable 1> <init 1>) ...),

and <body> should be a sequence of one or more expressions.

Semantics: Let* is similar to let, but the bindings are performed sequentially from left to right, and the region of a binding indicated by (<variable> <init>) is that part of the let* expression to the right of the binding. Thus the second binding is done in an environment in which the first binding is visible, and so on.

(let ((x 2) (y 3))
  (let* ((x 7)
         (z (+ x y)))
    (* z x)))               =>  70

essential syntax: letrec <bindings> <body>

Syntax: <Bindings> should have the form

((<variable 1> <init 1>) ...),

and <body> should be a sequence of one or more expressions. It is an error for a <variable> to appear more than once in the list of variables being bound.

Semantics: The <variable>s are bound to fresh locations holding undefined values, the <init>s are evaluated in the resulting environment (in some unspecified order), each <variable> is assigned to the result of the corresponding <init>, the <body> is evaluated in the resulting environment, and the value of the last expression in <body> is returned. Each binding of a <variable> has the entire letrec expression as its region , making it possible to define mutually recursive procedures.


(letrec ((even?
          (lambda (n)
            (if (zero? n)
                #t
                (odd? (- n 1)))))
         (odd?
          (lambda (n)
            (if (zero? n)
                #f
                (even? (- n 1))))))
  (even? 88))
                            =>  #t

One restriction on letrec is very important: it must be possible to evaluate each <init> without assigning or referring to the value of any <variable>. If this restriction is violated, then it is an error. The restriction is necessary because Scheme passes arguments by value rather than by name. In the most common uses of letrec, all the <init>s are lambda expressions and the restriction is satisfied automatically.

Sequencing

essential syntax: begin <expression 1> <expression 2> ...

The <expression>s are evaluated sequentially from left to right, and the value of the last <expression> is returned. This expression type is used to sequence side effects such as input and output.

(define x 0)

(begin (set! x 5)
       (+ x 1))             =>  6

(begin (display "4 plus 1 equals ")
       (display (+ 4 1)))   =>  unspecified
        and prints  4 plus 1 equals 5

Note: [SICP] uses the keyword sequence instead of begin.

Iteration

syntax: do <bindings> <clause> <body>

Syntax: <Bindings> should have the form

((<variable 1> <init 1> <step 1>) ...),

<clause> should be of the form

(<test> <expression> ...),

and <body> should be a sequence of one or more expressions.

Do is an iteration construct. It specifies a set of variables to be bound, how they are to be initialized at the start, and how they are to be updated on each iteration. When a termination condition is met, the loop exits with a specified result value.

Do expressions are evaluated as follows: The <init> expressions are evaluated (in some unspecified order), the <variable>s are bound to fresh locations, the results of the <init> expressions are stored in the bindings of the <variable>s, and then the iteration phase begins.

Each iteration begins by evaluating <test>; if the result is false (see section Booleans), then the <body> expressions are evaluated in order for effect, the <step> expressions are evaluated in some unspecified order, the <variable>s are bound to fresh locations, the results of the <step>s are stored in the bindings of the <variable>s, and the next iteration begins.

If <test> evaluates to a true value, then the <expression>s are evaluated from left to right and the value of the last <expression> is returned as the value of the do expression. If no <expression>s are present, then the value of the do expression is unspecified.

The region of the binding of a <variable> consists of the entire do expression except for the <init>s. It is an error for a <variable> to appear more than once in the list of do variables.

A <step> may be omitted, in which case the effect is the same as if (<variable> <init> <variable>) had been written instead of (<variable> <init>).

(do ((vec (make-vector 5))
     (i 0 (+ i 1)))
    ((= i 5) vec)
  (vector-set! vec i i))    =>  #(0 1 2 3 4)

(let ((x '(1 3 5 7 9)))
  (do ((x x (cdr x))
       (sum 0 (+ sum (car x))))
      ((null? x) sum)))     =>  25

syntax: let <variable> <bindings> <body>

Some implementations of Scheme permit a variant on the syntax of let called "named let" which provides a more general looping construct than do, and may also be used to express recursions.

Named let has the same syntax and semantics as ordinary let except that <variable> is bound within <body> to a procedure whose formal arguments are the bound variables and whose body is <body>. Thus the execution of <body> may be repeated by invoking the procedure named by <variable>.

(let loop ((numbers '(3 -2 1 6 -5))
           (nonneg '())
           (neg '()))
  (cond ((null? numbers) (list nonneg neg))
        ((>= (car numbers) 0)
         (loop (cdr numbers)
               (cons (car numbers) nonneg)
               neg))
        ((< (car numbers) 0)
         (loop (cdr numbers)
               nonneg
               (cons (car numbers) neg)))))
                            =>  ((6 1 3) (-5 -2))

Delayed evaluation

syntax: delay <expression>

The delay construct is used together with the procedure force to implement lazy evaluation or call by need. (delay <expression>) returns an object called a promise which at some point in the future may be asked (by the force procedure) to evaluate <expression> and deliver the resulting value.

See the description of force (section Control features) for a more complete description of delay.

Quasiquotation

essential syntax: quasiquote <template>

essential syntax: ` <template>

"Backquote" or "quasiquote" expressions are useful for constructing a list or vector structure when most but not all of the desired structure is known in advance. If no commas appear within the <template>, the result of evaluating `<template> is equivalent to the result of evaluating '<template>. If a comma appears within the <template>, however, the expression following the comma is evaluated ("unquoted") and its result is inserted into the structure instead of the comma and the expression. If a comma appears followed immediately by an at-sign (@), then the following expression must evaluate to a list; the opening and closing parentheses of the list are then "stripped away" and the elements of the list are inserted in place of the comma at-sign expression sequence.

`(list ,(+ 1 2) 4)          =>  (list 3 4)
(let ((name 'a)) `(list ,name ',name))
                            =>  (list a (quote a))
`(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)
                            =>  (a 3 4 5 6 b)
`((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))
                            =>  ((foo 7) . cons)
`#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8)
                            =>  #(10 5 2 4 3 8)

Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing at the same nesting level as the outermost backquote. The nesting level increases by one inside each successive quasiquotation, and decreases by one inside each unquotation.

`(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)
                            =>  (a `(b ,(+ 1 2) ,(foo 4 d) e) f)
(let ((name1 'x)
      (name2 'y))
  `(a `(b ,,name1 ,',name2 d) e))
                            =>  (a `(b ,x ,'y d) e)

The notations `<template> and (quasiquote <template>) are identical in all respects. ,<expression> is identical to (unquote <expression>), and ,<expression> is identical to (unquote-splicing <expression>). The external syntax generated by write for two-element lists whose car is one of these symbols may vary between implementations.

(quasiquote (list (unquote (+ 1 2)) 4))
                            =>  (list 3 4)
'(quasiquote (list (unquote (+ 1 2)) 4))
                            =>  `(list ,(+ 1 2) 4)
     i.e., (quasiquote (list (unquote (+ 1 2)) 4))

Unpredictable behavior can result if any of the symbols quasiquote, unquote, or unquote-splicing appear in positions within a <template> otherwise than as described above.

Program structure

Programs

A Scheme program consists of a sequence of expressions and definitions. Expressions are described in section Expressions; definitions are the subject of the rest of the present chapter.

Programs are typically stored in files or entered interactively to a running Scheme system, although other paradigms are possible; questions of user interface lie outside the scope of this report. (Indeed, Scheme would still be useful as a notation for expressing computational methods even in the absence of a mechanical implementation.)

Definitions occurring at the top level of a program can be interpreted declaratively. They cause bindings to be created in the top level environment. Expressions occurring at the top level of a program are interpreted imperatively; they are executed in order when the program is invoked or loaded, and typically perform some kind of initialization.

Definitions

Definitions are valid in some, but not all, contexts where expressions are allowed. They are valid only at the top level of a <program> and, in some implementations, at the beginning of a <body>.

A definition should have one of the following forms:

Top level definitions

At the top level of a program, a definition

(define <variable> <expression>)

has essentially the same effect as the assignment expression

(set! <variable> <expression>)

if <variable> is bound. If <variable> is not bound, however, then the definition will bind <variable> to a new location before performing the assignment, whereas it would be an error to perform a set! on an unbound variable.

(define add3
  (lambda (x) (+ x 3)))
(add3 3)                    =>  6
(define first car)
(first '(1 2))              =>  1

All Scheme implementations must support top level definitions.

Some implementations of Scheme use an initial environment in which all possible variables are bound to locations, most of which contain undefined values. Top level definitions in such an implementation are truly equivalent to assignments.

Internal definitions

Some implementations of Scheme permit definitions to occur at the beginning of a <body> (that is, the body of a lambda, let, let*, letrec, or define expression). Such definitions are known as internal definitions as opposed to the top level definitions described above. The variable defined by an internal definition is local to the <body>. That is, <variable> is bound rather than assigned, and the region of the binding is the entire <body>. For example,

(let ((x 5))
  (define foo (lambda (y) (bar x y)))
  (define bar (lambda (a b) (+ (* a b) a)))
  (foo (+ x 3)))            =>  45

A <body> containing internal definitions can always be converted into a completely equivalent letrec expression. For example, the let expression in the above example is equivalent to

(let ((x 5))
  (letrec ((foo (lambda (y) (bar x y)))
           (bar (lambda (a b) (+ (* a b) a))))
    (foo (+ x 3))))

Just as for the equivalent letrec expression, it must be possible to evaluate each <expression> of every internal definition in a <body> without assigning or referring to the value of any <variable> being defined.

Standard procedures

This chapter describes Scheme's built-in procedures. The initial (or "top level") Scheme environment starts out with a number of variables bound to locations containing useful values, most of which are primitive procedures that manipulate data. For example, the variable abs is bound to (a location initially containing) a procedure of one argument that computes the absolute value of a number, and the variable + is bound to a procedure that computes sums.

Booleans

The standard boolean objects for true and false are written as #t and #f. What really matters, though, are the objects that the Scheme conditional expressions (if, cond, and, or, do) treat as true or false. The phrase "a true value" (or sometimes just "true") means any object treated as true by the conditional expressions, and the phrase "a false value" (or "false") means any object treated as false by the conditional expressions.

Of all the standard Scheme values, only #f counts as false in conditional expressions. Except for #f, all standard Scheme values, including #t, pairs, the empty list, symbols, numbers, strings, vectors, and procedures, count as true.

Note: In some implementations the empty list counts as false, contrary to the above. Nonetheless a few examples in this report assume that the empty list counts as true, as in [IEEESCHEME].

Note: Programmers accustomed to other dialects of Lisp should be aware that Scheme distinguishes both #f and the empty list from the symbol nil.

Boolean constants evaluate to themselves, so they don't need to be quoted in programs.

#t                          =>  #t
#f                          =>  #f
'#f                         =>  #f

essential procedure: not obj

Not returns #t if obj is false, and returns #f otherwise.

(not #t)                    =>  #f
(not 3)                     =>  #f
(not (list 3))              =>  #f
(not #f)                    =>  #t
(not '())                   =>  #f
(not (list))                =>  #f
(not 'nil)                  =>  #f

essential procedure: boolean? obj

Boolean? returns #t if obj is either #t or #f and returns #f otherwise.

(boolean? #f)               =>  #t
(boolean? 0)                =>  #f
(boolean? '())              =>  #f

Equivalence predicates

A predicate is a procedure that always returns a boolean value (#t or #f). An equivalence predicate is the computational analogue of a mathematical equivalence relation (it is symmetric, reflexive, and transitive). Of the equivalence predicates described in this section, eq? is the finest or most discriminating, and equal? is the coarsest. Eqv? is slightly less discriminating than eq?.

essential procedure: eqv? obj1 obj2

The eqv? procedure defines a useful equivalence relation on objects. Briefly, it returns #t if obj1 and obj2 should normally be regarded as the same object. This relation is left slightly open to interpretation, but the following partial specification of eqv? holds for all implementations of Scheme.

The eqv? procedure returns #t if:

The eqv? procedure returns #f if:

(eqv? 'a 'a)                =>  #t
(eqv? 'a 'b)                =>  #f
(eqv? 2 2)                  =>  #t
(eqv? '() '())              =>  #t
(eqv? 100000000 100000000)  =>  #t
(eqv? (cons 1 2) (cons 1 2))=>  #f
(eqv? (lambda () 1)
      (lambda () 2))        =>  #f
(eqv? #f 'nil)              =>  #f
(let ((p (lambda (x) x)))
  (eqv? p p))               =>  #t

The following examples illustrate cases in which the above rules do not fully specify the behavior of eqv?. All that can be said about such cases is that the value returned by eqv? must be a boolean.

(eqv? "" "")                =>  unspecified
(eqv? '#() '#())            =>  unspecified
(eqv? (lambda (x) x)
      (lambda (x) x))       =>  unspecified
(eqv? (lambda (x) x)
      (lambda (y) y))       =>  unspecified

The next set of examples shows the use of eqv? with procedures that have local state. Gen-counter must return a distinct procedure every time, since each procedure has its own internal counter. Gen-loser, however, returns equivalent procedures each time, since the local state does not affect the value or side effects of the procedures.

(define gen-counter
  (lambda ()
    (let ((n 0))
      (lambda () (set! n (+ n 1)) n))))
(let ((g (gen-counter)))
  (eqv? g g))               =>  #t
(eqv? (gen-counter) (gen-counter))
                            =>  #f
(define gen-loser
  (lambda ()
    (let ((n 0))
      (lambda () (set! n (+ n 1)) 27))))
(let ((g (gen-loser)))
  (eqv? g g))               =>  #t
(eqv? (gen-loser) (gen-loser))
                            =>  unspecified

(letrec ((f (lambda () (if (eqv? f g) 'both 'f)))
         (g (lambda () (if (eqv? f g) 'both 'g)))
  (eqv? f g))
                            =>  unspecified

(letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
         (g (lambda () (if (eqv? f g) 'g 'both)))
  (eqv? f g))
                            =>  #f

Since it is an error to modify constant objects (those returned by literal expressions), implementations are permitted, though not required, to share structure between constants where appropriate. Thus the value of eqv? on constants is sometimes implementation-dependent.

(eqv? '(a) '(a))            =>  unspecified
(eqv? "a" "a")              =>  unspecified
(eqv? '(b) (cdr '(a b)))    =>  unspecified
(let ((x '(a)))
  (eqv? x x))               =>  #t

Rationale: The above definition of eqv? allows implementations latitude in their treatment of procedures and literals: implementations are free either to detect or to fail to detect that two procedures or two literals are equivalent to each other, and can decide whether or not to merge representations of equivalent objects by using the same pointer or bit pattern to represent both.

essential procedure: eq? obj1 obj2

Eq? is similar to eqv? except that in some cases it is capable of discerning distinctions finer than those detectable by eqv?.

Eq? and eqv? are guaranteed to have the same behavior on symbols, booleans, the empty list, pairs, and non-empty strings and vectors. Eq?'s behavior on numbers and characters is implementation-dependent, but it will always return either true or false, and will return true only when eqv? would also return true. Eq? may also behave differently from eqv? on empty vectors and empty strings.

(eq? 'a 'a)                 =>  #t
(eq? '(a) '(a))             =>  unspecified
(eq? (list 'a) (list 'a))   =>  #f
(eq? "a" "a")               =>  unspecified
(eq? "" "")                 =>  unspecified
(eq? '() '())               =>  #t
(eq? 2 2)                   =>  unspecified
(eq? #\A #\A)               =>  unspecified
(eq? car car)               =>  #t
(let ((n (+ 2 3)))
  (eq? n n))                =>  unspecified
(let ((x '(a)))
  (eq? x x))                =>  #t
(let ((x '#()))
  (eq? x x))                =>  #t
(let ((p (lambda (x) x)))
  (eq? p p))                =>  #t

Rationale: It will usually be possible to implement eq? much more efficiently than eqv?, for example, as a simple pointer comparison instead of as some more complicated operation. One reason is that it may not be possible to compute eqv? of two numbers in constant time, whereas eq? implemented as pointer comparison will always finish in constant time. Eq? may be used like eqv? in applications using procedures to implement objects with state since it obeys the same constraints as eqv?.

essential procedure: equal? obj1 obj2

Equal? recursively compares the contents of pairs, vectors, and strings, applying eqv? on other objects such as numbers and symbols. A rule of thumb is that objects are generally equal? if they print the same. Equal? may fail to terminate if its arguments are circular data structures.

(equal? 'a 'a)              =>  #t
(equal? '(a) '(a))          =>  #t
(equal? '(a (b) c)
        '(a (b) c))         =>  #t
(equal? "abc" "abc")        =>  #t
(equal? 2 2)                =>  #t
(equal? (make-vector 5 'a)
        (make-vector 5 'a)) =>  #t
(equal? (lambda (x) x)
        (lambda (y) y))     =>  unspecified

Pairs and lists

A pair (sometimes called a dotted pair) is a record structure with two fields called the car and cdr fields (for historical reasons). Pairs are created by the procedure cons. The car and cdr fields are accessed by the procedures car and cdr. The car and cdr fields are assigned by the procedures set-car! and set-cdr!.

Pairs are used primarily to represent lists. A list can be defined recursively as either the empty list or a pair whose cdr is a list. More precisely, the set of lists is defined as the smallest set X such that

The objects in the car fields of successive pairs of a list are the elements of the list. For example, a two-element list is a pair whose car is the first element and whose cdr is a pair whose car is the second element and whose cdr is the empty list. The length of a list is the number of elements, which is the same as the number of pairs.

The empty list is a special object of its own type (it is not a pair); it has no elements and its length is zero.

Note: The above definitions imply that all lists have finite length and are terminated by the empty list.

The most general notation (external representation) for Scheme pairs is the "dotted" notation (c1 . c2) where c1 is the value of the car field and c2 is the value of the cdr field. For example (4 . 5) is a pair whose car is 4 and whose cdr is 5. Note that (4 . 5) is the external representation of a pair, not an expression that evaluates to a pair.

A more streamlined notation can be used for lists: the elements of the list are simply enclosed in parentheses and separated by spaces. The empty list is written (). For example,

(a b c d e)

and

(a . (b . (c . (d . (e . ())))))

are equivalent notations for a list of symbols.

A chain of pairs not ending in the empty list is called an improper list. Note that an improper list is not a list. The list and dotted notations can be combined to represent improper lists:

(a b c . d)

is equivalent to

(a . (b . (c . d)))

Whether a given pair is a list depends upon what is stored in the cdr field. When the set-cdr! procedure is used, an object can be a list one moment and not the next:

(define x (list 'a 'b 'c))
(define y x)
y                           =>  (a b c)
(list? y)                   =>  #t
(set-cdr! x 4)              =>  unspecified
x                           =>  (a . 4)
(eqv? x y)                  =>  #t
y                           =>  (a . 4)
(list? y)                   =>  #f
(set-cdr! x x)              =>  unspecified
(list? x)                   =>  #f

Within literal expressions and representations of objects read by the read procedure, the forms '<datum>,`<datum>, ,<datum>, and ,@<datum> denote two-element lists whose first elements are the symbols quote, quasiquote, unquote, and unquote-splicing, respectively. The second element in each case is <datum>. This convention is supported so that arbitrary Scheme programs may be represented as lists. That is, according to Scheme's grammar, every <expression> is also a <datum> (see section External representations). Among other things, this permits the use of the read procedure to parse Scheme programs. See section External representations.

essential procedure: pair? obj

Pair? returns #t if obj is a pair, and otherwise returns #f.

(pair? '(a . b))            =>  #t
(pair? '(a b c))            =>  #t
(pair? '())                 =>  #f
(pair? '#(a b))             =>  #f

essential procedure: cons obj1 obj2

Returns a newly allocated pair whose car is obj1 and whose cdr is obj2. The pair is guaranteed to be different (in the sense of eqv?) from every existing object.

(cons 'a '())               =>  (a)
(cons '(a) '(b c d))        =>  ((a) b c d)
(cons "a" '(b c))           =>  ("a" b c)
(cons 'a 3)                 =>  (a . 3)
(cons '(a b) 'c)            =>  ((a b) . c)

essential procedure: car pair

Returns the contents of the car field of pair. Note that it is an error to take the car of the empty list.

(car '(a b c))              =>  a
(car '((a) b c d))          =>  (a)
(car '(1 . 2))              =>  1
(car '())                   =>  error

essential procedure: cdr pair

Returns the contents of the cdr field of pair. Note that it is an error to take the cdr of the empty list.

(cdr '((a) b c d))          =>  (b c d)
(cdr '(1 . 2))              =>  2
(cdr '())                   =>  error

essential procedure: set-car! pair obj

Stores obj in the car field of pair. The value returned by set-car! is unspecified.

(define (f) (list 'not-a-constant-list))
(define (g) '(constant-list))
(set-car! (f) 3)            =>  unspecified
(set-car! (g) 3)            =>  error

essential procedure: set-cdr! pair obj

Stores obj in the cdr field of pair. The value returned by set-cdr! is unspecified.

essential procedure: caar pair

essential procedure: cadr pair

... essential procedure: cdddar pair

essential procedure: cddddr pair

These procedures are compositions of car and cdr, where for example caddr could be defined by

(define caddr (lambda (x) (car (cdr (cdr x))))).

Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all.

essential procedure: null? obj

Returns #t if obj is the empty list, otherwise returns #f.

essential procedure: list? obj

Returns #t if obj is a list, otherwise returns #f. By definition, all lists have finite length and are terminated by the empty list.

        (list? '(a b c))    =>  #t
        (list? '())         =>  #t
        (list? '(a . b))    =>  #f
        (let ((x (list 'a)))
          (set-cdr! x x)
          (list? x))        =>  #f

essential procedure: list obj ...

Returns a newly allocated list of its arguments.

(list 'a (+ 3 4) 'c)        =>  (a 7 c)
(list)                      =>  ()

essential procedure: length list

Returns the length of list.

(length '(a b c))           =>  3
(length '(a (b) (c d e)))   =>  3
(length '())                =>  0

essential procedure: append list ...

Returns a list consisting of the elements of the first list followed by the elements of the other lists.

(append '(x) '(y))          =>  (x y)
(append '(a) '(b c d))      =>  (a b c d)
(append '(a (b)) '((c)))    =>  (a (b) (c))

The resulting list is always newly allocated, except that it shares structure with the last list argument. The last argument may actually be any object; an improper list results if the last argument is not a proper list.

(append '(a b) '(c . d))    =>  (a b c . d)
(append '() 'a)             =>  a

essential procedure: reverse list

Returns a newly allocated list consisting of the elements of list in reverse order.

(reverse '(a b c))          =>  (c b a)
(reverse '(a (b c) d (e (f))))  
                            =>  ((e (f)) d (b c) a)

procedure: list-tail list k

Returns the sublist of list obtained by omitting the first k elements. List-tail could be defined by

(define list-tail
  (lambda (x k)
    (if (zero? k)
        x
        (list-tail (cdr x) (- k 1)))))

essential procedure: list-ref list k

Returns the kth element of list. (This is the same as the car of (list-tail list k).)

(list-ref '(a b c d) 2)     =>  c
(list-ref '(a b c d)
          (inexact->exact (round 1.8))) 
                            =>  c

essential procedure: memq obj list

essential procedure: memv obj list

essential procedure: member obj list

These procedures return the first sublist of list whose car is obj, where the sublists of list are the non-empty lists returned by (list-tail list k) for k less than the length of list. If obj does not occur in list, then #f (not the empty list) is returned. Memq uses eq? to compare obj with the elements of list, while memv uses eqv? and member uses equal?.

(memq 'a '(a b c))          =>  (a b c)
(memq 'b '(a b c))          =>  (b c)
(memq 'a '(b c d))          =>  #f
(memq (list 'a) '(b (a) c)) =>  #f
(member (list 'a)
        '(b (a) c))         =>  ((a) c)
(memq 101 '(100 101 102))   =>  unspecified
(memv 101 '(100 101 102))   =>  (101 102)

essential procedure: assq obj alist

essential procedure: assv obj alist

essential procedure: assoc obj alist

Alist (for "association list") must be a list of pairs. These procedures find the first pair in alist whose car field is obj, and returns that pair. If no pair in alist has obj as its car, then #f (not the empty list) is returned. Assq uses eq? to compare obj with the car fields of the pairs in alist, while assv uses eqv? and assoc uses equal?.

(define e '((a 1) (b 2) (c 3)))
(assq 'a e)                 =>  (a 1)
(assq 'b e)                 =>  (b 2)
(assq 'd e)                 =>  #f
(assq (list 'a) '(((a)) ((b)) ((c))))
                            =>  #f
(assoc (list 'a) '(((a)) ((b)) ((c))))
                            =>  ((a))
(assq 5 '((2 3) (5 7) (11 13)))
                            =>  unspecified
(assv 5 '((2 3) (5 7) (11 13)))
                            =>  (5 7)

Rationale: Although they are ordinarily used as predicates, memq, memv, member, assq, assv, and symbolsassoc do not have question marks in their names because they return useful values rather than just #t or #f.

Symbols

Symbols are objects whose usefulness rests on the fact that two symbols are identical (in the sense of eqv?) if and only if their names are spelled the same way. This is exactly the property needed to represent identifiers in programs, and so most implementations of Scheme use them internally for that purpose. Symbols are useful for many other applications; for instance, they may be used the way enumerated values are used in Pascal.

The rules for writing a symbol are exactly the same as the rules for writing an identifier; see section Identifiers and section Lexical structure.

It is guaranteed that any symbol that has been returned as part of a literal expression, or read using the read procedure, and subsequently written out using the write procedure, will read back in as the identical symbol (in the sense of eqv?). The string->symbol procedure, however, can create symbols for which this write/read invariance may not hold because their names contain special characters or letters in the non-standard case.

Note: Some implementations of Scheme have a feature known as "slashification" in order to guarantee write/read invariance for all symbols, but historically the most important use of this feature has been to compensate for the lack of a string data type.

Some implementations also have "uninterned symbols", which defeat write/read invariance even in implementations with slashification, and also generate exceptions to the rule that two symbols are the same if and only if their names are spelled the same.

essential procedure: symbol? obj

Returns #t if obj is a symbol, otherwise returns #f.

(symbol? 'foo)              =>  #t
(symbol? (car '(a b)))      =>  #t
(symbol? "bar")             =>  #f
(symbol? 'nil)              =>  #t
(symbol? '())               =>  #f
(symbol? #f)                =>  #f

essential procedure: symbol->string symbol

Returns the name of symbol as a string. If the symbol was part of an object returned as the value of a literal expression (section Literal expressions) or by a call to the read procedure, and its name contains alphabetic characters, then the string returned will contain characters in the implementation's preferred standard case--some implementations will prefer upper case, others lower case. If the symbol was returned by string->symbol, the case of characters in the string returned will be the same as the case in the string that was passed to string->symbol. It is an error to apply mutation procedures like string-set! to strings returned by this procedure.

The following examples assume that the implementation's standard case is lower case:

(symbol->string 'flying-fish)
                            =>  "flying-fish"
(symbol->string 'Martin)    =>  "martin"
(symbol->string
   (string->symbol "Malvina"))
                            =>  "Malvina"

essential procedure: string->symbol string

Returns the symbol whose name is string. This procedure can create symbols with names containing special characters or letters in the non-standard case, but it is usually a bad idea to create such symbols because in some implementations of Scheme they cannot be read as themselves. See symbol->string.

The following examples assume that the implementation's standard case is lower case:

(eq? 'mISSISSIppi 'mississippi)  
                            =>  #t
(string->symbol "mISSISSIppi")  
                            =>  the symbol with name "mISSISSIppi"
(eq? 'bitBlt (string->symbol "bitBlt"))     
                            =>  #f
(eq? 'JollyWog
     (string->symbol
       (symbol->string 'JollyWog)))  
                            =>  #t
(string=? "K. Harper, M.D."
          (symbol->string
            (string->symbol "K. Harper, M.D.")))  
                            =>  #t

Numbers

Numerical computation has traditionally been neglected by the Lisp community. Until Common Lisp there was no carefully thought out strategy for organizing numerical computation, and with the exception of the MacLisp system [PITMAN83] little effort was made to execute numerical code efficiently. This report recognizes the excellent work of the Common Lisp committee and accepts many of their recommendations. In some ways this report simplifies and generalizes their proposals in a manner consistent with the purposes of Scheme.

It is important to distinguish between the mathematical numbers, the Scheme numbers that attempt to model them, the machine representations used to implement the Scheme numbers, and notations used to write numbers. This report uses the types number, complex, real, rational, and integer to refer to both mathematical numbers and Scheme numbers. Machine representations such as fixed point and floating point are referred to by names such as fixnum and flonum.

Numerical types

Mathematically, numbers may be arranged into a tower of subtypes in which each level is a subset of the level above it:

For example, 3 is an integer. Therefore 3 is also a rational, a real, and a complex. The same is true of the Scheme numbers that model 3. For Scheme numbers, these types are defined by the predicates number?, complex?, real?, rational?, and integer?.

There is no simple relationship between a number's type and its representation inside a computer. Although most implementations of Scheme will offer at least two different representations of 3, these different representations denote the same integer.

Scheme's numerical operations treat numbers as abstract data, as independent of their representation as possible. Although an implementation of Scheme may use fixnum, flonum, and perhaps other representations for numbers, this should not be apparent to a casual programmer writing simple programs.

It is necessary, however, to distinguish between numbers that are represented exactly and those that may not be. For example, indexes into data structures must be known exactly, as must some polynomial coefficients in a symbolic algebra system. On the other hand, the results of measurements are inherently inexact, and irrational numbers may be approximated by rational and therefore inexact approximations. In order to catch uses of inexact numbers where exact numbers are required, Scheme explicitly distinguishes exact from inexact numbers. This distinction is orthogonal to the dimension of type.

Exactness

Scheme numbers are either exact or inexact. A number is exact if it was written as an exact constant or was derived from exact numbers using only exact operations. A number is inexact if it was written as an inexact constant, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number.

If two implementations produce exact results for a computation that did not involve inexact intermediate results, the two ultimate results will be mathematically equivalent. This is generally not true of computations involving inexact numbers since approximate methods such as floating point arithmetic may be used, but it is the duty of each implementation to make the result as close as practical to the mathematically ideal result.

Rational operations such as + should always produce exact results when given exact arguments. If the operation is unable to produce an exact result, then it may either report the violation of an implementation restriction or it may silently coerce its result to an inexact value. See section Implementation restrictions.

With the exception of inexact->exact, the operations described in this section must generally return inexact results when given any inexact arguments. An operation may, however, return an exact result if it can prove that the value of the result is unaffected by the inexactness of its arguments. For example, multiplication of any number by an exact zero may produce an exact zero result, even if the other argument is inexact.

Implementation restrictions

Implementations of Scheme are not required to implement the whole tower of subtypes given in section Numerical types, but they must implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language. For example, an implementation in which all numbers are real may still be quite useful.

Implementations may also support only a limited range of numbers of any type, subject to the requirements of this section. The supported range for exact numbers of any type may be different from the supported range for inexact numbers of that type. For example, an implementation that uses flonums to represent all its inexact real numbers may support a practically unbounded range of exact integers and rationals while limiting the range of inexact reals (and therefore the range of inexact integers and rationals) to the dynamic range of the flonum format. Furthermore the gaps between the representable inexact integers and rationals are likely to be very large in such an implementation as the limits of this range are approached.

An implementation of Scheme must support exact integers throughout the range of numbers that may be used for indexes of lists, vectors, and strings or that may result from computing the length of a list, vector, or string. The length, vector-length, and string-length procedures must return an exact integer, and it is an error to use anything but an exact integer as an index. Furthermore any integer constant within the index range, if expressed by an exact integer syntax, will indeed be read as an exact integer, regardless of any implementation restrictions that may apply outside this range. Finally, the procedures listed below will always return an exact integer result provided all their arguments are exact integers and the mathematically expected result is representable as an exact integer within the implementation:

+            -             *
quotient     remainder     modulo
max          min           abs
numerator    denominator   gcd
lcm          floor         ceiling
truncate     round         rationalize
expt

Implementations are encouraged, but not required, to support exact integers and exact rationals of practically unlimited size and precision, and to implement the above procedures and the / procedure in such a way that they always return exact results when given exact arguments. If one of these procedures is unable to deliver an exact result when given exact arguments, then it may either report a violation of an implementation restriction or it may silently coerce its result to an inexact number. Such a coercion may cause an error later.

An implementation may use floating point and other approximate representation strategies for inexact numbers.

This report recommends, but does not require, that the IEEE 32-bit and 64-bit floating point standards be followed by implementations that use flonum representations, and that implementations using other representations should match or exceed the precision achievable using these floating point standards [IEEE].

In particular, implementations that use flonum representations must follow these rules: A flonum result must be represented with at least as much precision as is used to express any of the inexact arguments to that operation. It is desirable (but not required) for potentially inexact operations such as sqrt, when applied to exact arguments, to produce exact answers whenever possible (for example the square root of an exact 4 ought to be an exact 2). If, however, an exact number is operated upon so as to produce an inexact result (as by sqrt), and if the result is represented as a flonum, then the most precise flonum format available must be used; but if the result is represented in some other way then the representation must have at least as much precision as the most precise flonum format available.

Although Scheme allows a variety of written notations for numbers, any particular implementation may support only some of them. For example, an implementation in which all numbers are real need not support the rectangular and polar notations for complex numbers. If an implementation encounters an exact numerical constant that it cannot represent as an exact number, then it may either report a violation of an implementation restriction or it may silently represent the constant by an inexact number.

Syntax of numerical constants

The syntax of the written representations for numbers is described formally in section Lexical structure.

A number may be written in binary, octal, decimal, or hexadecimal by the use of a radix prefix. The radix prefixes are #b (binary), #o (octal), #d (decimal), and #x (hexadecimal). With no radix prefix, a number is assumed to be expressed in decimal.

A numerical constant may be specified to be either exact or inexact by a prefix. The prefixes are #e for exact, and #i for inexact. An exactness prefix may appear before or after any radix prefix that is used. If the written representation of a number has no exactness prefix, the constant may be either inexact or exact. It is inexact if it contains a decimal point, an exponent, or a "#" character in the place of a digit, otherwise it is exact.

In systems with inexact numbers of varying precisions it may be useful to specify the precision of a constant. For this purpose, numerical constants may be written with an exponent marker that indicates the desired precision of the inexact representation. The letters s, f, d, and l specify the use of short, single, double, and long precision, respectively. (When fewer than four internal inexact representations exist, the four size specifications are mapped onto those available. For example, an implementation with two internal representations may map short and single together and long and double together.) In addition, the exponent marker e specifies the default precision for the implementation. The default precision has at least as much precision as double, but implementations may wish to allow this default to be set by the user.

3.14159265358979F0
        Round to single --- 3.141593
0.6L0
        Extend to long --- .600000000000000

Numerical operations

The reader is referred to section Entry format for a summary of the naming conventions used to specify restrictions on the types of arguments to numerical routines.

The examples used in this section assume that any numerical constant written using an exact notation is indeed represented as an exact number. Some examples also assume that certain numerical constants written using an inexact notation can be represented without loss of accuracy; the inexact constants were chosen so that this is likely to be true in implementations that use flonums to represent inexact numbers.

essential procedure: number? obj

essential procedure: complex? obj

essential procedure: real? obj

essential procedure: rational? obj

essential procedure: integer? obj

These numerical type predicates can be applied to any kind of argument, including non-numbers. They return #t if the object is of the named type, and otherwise they return #f. In general, if a type predicate is true of a number then all higher type predicates are also true of that number. Consequently, if a type predicate is false of a number, then all lower type predicates are also false of that number.

If z is an inexact complex number, then (real? z) is true if and only if (zero? (imag-part z)) is true. If x is an inexact real number, then (integer? x) is true if and only if (= x (round x)).

(complex? 3+4i)             =>  #t
(complex? 3)                =>  #t
(real? 3)                   =>  #t
(real? -2.5+0.0i)           =>  #t
(real? #e1e10)              =>  #t
(rational? 6/10)            =>  #t
(rational? 6/3)             =>  #t
(integer? 3+0i)             =>  #t
(integer? 3.0)              =>  #t
(integer? 8/4)              =>  #t

Note: The behavior of these type predicates on inexact numbers is unreliable, since any inaccuracy may affect the result.

Note: In many implementations the rational? procedure will be the same as real?, and the complex? procedure will be the same as number?, but unusual implementations may be able to represent some irrational numbers exactly or may extend the number system to support some kind of non-complex numbers.

essential procedure: exact? z

essential procedure: inexact? z

These numerical predicates provide tests for the exactness of a quantity. For any Scheme number, precisely one of these predicates is true.

essential procedure: = z1 z2 z3 ...

essential procedure: < x1 x2 x3 ...

essential procedure: > x1 x2 x3 ...

essential procedure: <= x1 x2 x3 ...

essential procedure: >= x1 x2 x3 ...

These procedures return #t if their arguments are (respectively): equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing.

These predicates are required to be transitive.

Note: The traditional implementations of these predicates in Lisp-like languages are not transitive.

Note: While it is not an error to compare inexact numbers using these predicates, the results may be unreliable because a small inaccuracy may affect the result; this is especially true of = and zero?. When in doubt, consult a numerical analyst.

essential procedure: zero? z

essential procedure: positive? x

essential procedure: negative? x

essential procedure: odd? n

essential procedure: even? n

These numerical predicates test a number for a particular property, returning #t or #f. See note above.

essential procedure: max x1 x2 ...

essential procedure: min x1 x2 ...

These procedures return the maximum or minimum of their arguments.

(max 3 4)                   =>  4    ; exact
(max 3.9 4)                 =>  4.0  ; inexact

Note: If any argument is inexact, then the result will also be inexact (unless the procedure can prove that the inaccuracy is not large enough to affect the result, which is possible only in unusual implementations). If min or max is used to compare numbers of mixed exactness, and the numerical value of the result cannot be represented as an inexact number without loss of accuracy, then the procedure may report a violation of an implementation restriction.

essential procedure: + z1 ...

essential procedure: * z1 ...

These procedures return the sum or product of their arguments.

(+ 3 4)                     =>  7
(+ 3)                       =>  3
(+)                         =>  0
(* 4)                       =>  4
(*)                         =>  1

essential procedure: - z1 z2

essential procedure: - z

procedure: - z1 z2 ...

essential procedure: / z1 z2

essential procedure: / z

procedure: / z1 z2 ...

With two or more arguments, these procedures return the difference or quotient of their arguments, associating to the left. With one argument, however, they return the additive or multiplicative inverse of their argument.

(- 3 4)                     =>  -1
(- 3 4 5)                   =>  -6
(- 3)                       =>  -3
(/ 3 4 5)                   =>  3/20
(/ 3)                       =>  1/3

essential procedure: abs x

Abs returns the magnitude of its argument.

(abs -7)                    =>  7

essential procedure: quotient n1 n2

essential procedure: remainder n1 n2

essential procedure: modulo n1 n2

These procedures implement number-theoretic (integer) division: For positive integers n1 and n2, if n3 and n4 are integers such that

(= n1 (+ (* n2 n3) n4)),
(<= 0 n4), and
(< n4 n2).

Then

(quotient n1 n2)            =>  n3
(remainder n1 n2)           =>  n4
(modulo n1 n2)              =>  n4

For integers n1 and n2 with n2 not equal to 0,

(= n1 (+ (* n2 (quotient n1 n2))
               (remainder n1 n2)))
                            =>  #t

provided all numbers involved in that computation are exact.

The value returned by quotient always has the sign of the product of its arguments. Remainder and modulo differ on negative arguments--the remainder is either zero or has the sign of the dividend, while the modulo always has the sign of the divisor:

(modulo 13 4)               =>  1
(remainder 13 4)            =>  1

(modulo -13 4)              =>  3
(remainder -13 4)           =>  -1

(modulo 13 -4)              =>  -3
(remainder 13 -4)           =>  1

(modulo -13 -4)             =>  -1
(remainder -13 -4)          =>  -1

(remainder -13 -4.0)        =>  -1.0  ; inexact

essential procedure: gcd n1 ...

essential procedure: lcm n1 ...

These procedures return the greatest common divisor or least common multiple of their arguments. The result is always non-negative.

(gcd 32 -36)                =>  4
(gcd)                       =>  0
(lcm 32 -36)                =>  288
(lcm 32.0 -36)              =>  288.0  ; inexact
(lcm)                       =>  1

procedure: numerator q

procedure: denominator q

These procedures return the numerator or denominator of their argument; the result is computed as if the argument was represented as a fraction in lowest terms. The denominator is always positive. The denominator of 0 is defined to be 1.

(numerator (/ 6 4))         =>  3
(denominator (/ 6 4))       =>  2
(denominator
  (exact->inexact (/ 6 4))) =>  2.0

essential procedure: floor x

essential procedure: ceiling x

essential procedure: truncate x

essential procedure: round x

These procedures return integers. Floor returns the largest integer not larger than x. Ceiling returns the smallest integer not smaller than x. Truncate returns the integer closest to x whose absolute value is not larger than the absolute value of x. Round returns the closest integer to x, rounding to even when x is halfway between two integers.

Rationale: Round rounds to even for consistency with the default rounding mode specified by the IEEE floating point standard.

Note: If the argument to one of these procedures is inexact, then the result will also be inexact. If an exact value is needed, the result should be passed to the inexact->exact procedure.

(floor -4.3)                =>  -5.0
(ceiling -4.3)              =>  -4.0
(truncate -4.3)             =>  -4.0
(round -4.3)                =>  -4.0

(floor 3.5)                 =>  3.0
(ceiling 3.5)               =>  4.0
(truncate 3.5)              =>  3.0
(round 3.5)                 =>  4.0  ; inexact

(round 7/2)                 =>  4    ; exact
(round 7)                   =>  7

procedure: rationalize x y

Rationalize returns the simplest rational number differing from x by no more than y. A rational number r1 is simpler than another rational number r2 if

(= r1 (/ p1 q1)) and
(= r2 (/ p2 q2)) (in lowest terms) and
(<= (abs p1) (abs p2)) and
(<= (abs q1) (abs q2)).

Thus (3/5) is simpler than (4/7). Although not all rationals are comparable in this ordering (consider (2/7) and (3/5)) any interval contains a rational number that is simpler than every other rational number in that interval (the simpler (2/5) lies between (2/7) and (3/5)). Note that 0 (0/1) is the simplest rational of all.

(rationalize
  (inexact->exact .3) 1/10)  =>  1/3    ; exact
(rationalize .3 1/10)        =>  #i1/3  ; inexact

procedure: exp z

procedure: log z

procedure: sin z

procedure: cos z

procedure: tan z

procedure: asin z

procedure: acos z

procedure: atan z

procedure: atan y x

These procedures are part of every implementation that supports general real numbers; they compute the usual transcendental functions. Log computes the natural logarithm of z (not the base ten logarithm). Asin, acos, and atan compute arcsine , arccosine , and arctangent , respectively. The two-argument variant of atan computes (angle (make-rectangular x y)) (see below), even in implementations that don't support general complex numbers.

In general, the mathematical functions log, arcsine, arccosine, and arctangent are multiply defined. For nonzero real x, the value of (log x) is defined to be the one whose imaginary part lies in the range -pi (exclusive) to pi (inclusive). (log 0) is undefined. The value of (log z) when z is complex is defined according to the formula

(define (log z) (+ (log (magnitude z)) (* +i (angle z))))

With (log) defined this way, the values of arcsin, arccos, and arctan are according to the following formulae:

(define (asin z) (* -i (log (+ (* +i z) (sqrt (- 1 (* z z)))))))

(define (acos z) (- (/ pi 2) (asin z)))

(define (atan z) (/ (log (/ (+ 1 (* +i z)) (- 1 (* +i z)))) (* +i 2))

The above specification follows [CLTL], which in turn cites [PENFIELD81]; refer to these sources for more detailed discussion of branch cuts, boundary conditions, and implementation of these functions. When it is possible these procedures produce a real result from a real argument.

procedure: sqrt z

Returns the principal square root of z. The result will have either positive real part, or zero real part and non-negative imaginary part.

procedure: expt z1 z2

Returns z1 raised to the power z2:

(define (expt z1 z2) (exp z2 (log z1)))

(expt 0 0) is defined to be equal to 1.

procedure: make-rectangular x1 x2

procedure: make-polar x3 x4

procedure: real-part z

procedure: imag-part z

procedure: magnitude z

procedure: angle z

These procedures are part of every implementation that supports general complex numbers. Suppose x1, x2, x3, and x4 are real numbers and z is a complex number such that

(= z (+ x1 (* +i x2) (* x3 (exp (* +i x4)))))

Then make-rectangular and make-polar return z, real-part returns x1, imag-part returns x2, magnitude returns x3, and angle returns x4. In the case of angle, whose value is not uniquely determined by the preceding rule, the value returned will be the one in the range -pi (exclusive) to pi (inclusive).

Rationale: Magnitude is the same as abs for a real argument, but abs must be