Revised(3) Report on the Algorithmic Language Scheme
****************************************************

begin-tabular

     JONATHAN REES AND WILLIAM CLINGER (Editors)
     H. ABELSON       R. K. DYBVIG     C. T. HAYNES     G. J. ROZAS
     N. I. ADAMS IV   D. P. FRIEDMAN   E. KOHLBECKER    G. J. SUSSMAN
     D. H. BARTLEY    R. HALSTEAD      D. OXLEY        M. WAND
     G. BROOKS              C. HANSON               K. M. PITMAN

Dedicated to the Memory of ALGOL 60

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.

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

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

Chapter Formal syntax and semantics provides a formal syntax for Scheme written in extended BNF, along with a formal denotational semantics.

The report concludes with an example of the use of the language and an alphabetic index.

Contents
********

Introduction
************

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 has influenced the evolution of Lisp.  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 known sequential control structures can be synthesized.  A few of these innovations have recently been incorporated into Common Lisp, while others remain to be adopted.

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 might be expected of a language used primarily for education and research, Scheme has always evolved rapidly.  This was no problem when Scheme was used only within MIT, but 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. Participating in this workshop were Hal Abelson, Norman Adams, David Bartley, Gary Brooks, William Clinger, Daniel Friedman, Robert Halstead, Chris Hanson, Christopher Haynes, Eugene Kohlbecker, Don Oxley, Jonathan Rees, Guillermo Rozas, Gerald Jay Sussman, and Mitchell Wand.  Kent Pitman made valuable contributions to the agenda for the workshop but was unable to attend the sessions.

Subsequent electronic mail discussions and committee work completed the definition of the language. Gerry Sussman drafted the section on numbers, Chris Hanson drafted the sections on characters and strings, and Gary Brooks and William Clinger drafted the sections on input and output. William Clinger recorded the decisions of the workshop and compiled the pieces into a coherent document. The ``Revised revised report on Scheme'' [RRRS] was published at MIT and Indiana University in the summer of 1985. Another round of revision in the spring of 1986, again accomplished almost entirely by electronic mail, resulted in the present report.

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 comments and criticisms: Alan Bawden, George Carrette, Andy Cromarty, Andy Freeman, Richard Gabriel, Yekta G"ursel, Ken Haase, Paul Hudak, Richard Kelsey, Chris Lindblad, Mark Meyer, Jim Miller, Jim Philbin, John Ramsdell, Guy Lewis Steele Jr., Julie Sussman, Perry Wagle, Daniel Weise, and Henry Wu.  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 and the Computer Science Department of Indiana University 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 chapters Basic concepts through 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 SASL, or the call-by-name semantics of Algol 60, where an argument expression is not evaluated unless its value is needed by the procedure.

Syntax
======

Scheme employs a parenthesized-list Polish notation to describe programs and (other) data.  The syntax of Scheme, like that of most Lisp dialects, provides for great expressive power, largely due to its simplicity.  An important consequence of this simplicity is the susceptibility of Scheme programs and data to uniform treatment by other Scheme programs.  As with other Lisp dialects, the read primitive parses its input; that is, it performs syntactic as well as lexical decomposition of what it reads.

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.

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 other arguments.

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
------------

Chapters Expressions and 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

      <thing1> ...

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

      <thing1> <thing2> ...

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: (append LIST1 LIST2)

 -- procedure: (append LIST ...,)

indicate that in all implementations, the append procedure must be defined to take two arguments, and some implementations will extend it to take zero or more 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, then this implies a restriction on the type of that argument to the procedure.  For example, the header line for vector-ref given above dictates that first argument to vector-ref must be a vector.  The following naming conventions also imply type restrictions:

begin-tabular

     OBJ
          any object

     Z, Z1, ... ZJ, ...
          complex, real, rational, integer

     X, X1, ... XJ, ...
          real, rational, integer

     Y, Y1, ... YJ, ...
          real, rational, integer

     Q, Q1, ... QJ, ...
          rational, integer

     N, N1, ... NJ, ...
          integer

     K, K1, ... KJ, ...
          exact non-negative integer

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.

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 - (which can begin numbers) 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 in identifiers exactly 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:

  * Certain identifiers are reserved for use as syntactic keywords (see below).

  * Any identifier that is not a syntactic keyword may be used as a variable (see section see Variables and regions).

  * When an identifier appears as a literal or within a literal (see section see Literal expressions), it is being used to denote a symbol (see section see Symbols).

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.

The characters ? and ! have no special properties---they are extended alphabetic characters.  By convention, however, most predicate procedures (those that return boolean values) are named by identifiers that end in ?, and most data mutation procedures are named by identifiers that end in !.

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 see Pairs and lists), and to indicate a rest-parameter in a  formal parameter list (section see lambda expressions).

( )
     Parentheses are used for grouping and to notate lists (section see Pairs and lists).

'
     The single quote character is used to indicate literal data (section see Literal expressions).

`
     The backquote character is used to indicate almost-constant data (section see Quasiquotation).

, ,@
     The character comma and the sequence comma at-sign are used in conjunction with backquote (section see Quasiquotation).

"
     The double quote character is used to delimit strings (section see Strings).

\
     Backslash is used in the syntax for character constants and as an escape character within string constants (section see 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 see Booleans).

#\
     This introduces a character constant (section see Characters).

#(
     This introduces a vector constant (section see Vectors).  Vector constants are terminated by ) .

#e #i #l #s #b #o #d #x
     These are used in the notation for numbers (section see Number syntax).

Basic concepts
**************

Variables and regions
=====================

Any identifier that is not a syntactic keyword (see section see 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 such 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 sections see lambda expressions, see Binding constructs, and see 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 see 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 and the empty list, which count as false.  This report uses the word ``true'' to refer to any Scheme value that counts as true, and the word ``false'' to refer to any Scheme value that counts as false.

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 ``28.000'' and ``#x1c'', and the list in the previous paragraph also has the representations ``( 08 13 )'' and ``(8 . (13 . ()))'' (see section see 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 see Literal expressions).

External representations can also be used for input and output.  The procedure read (section see Input) parses external representations, and the procedure write (section see 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 which 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 which 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 chapter Standard procedures.

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 type.  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 see 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 see 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)

     Numeric 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

     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> <operand1> ...,)

     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 indeterminate 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 see 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: 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 foo
            (let ((x 4))
              (lambda (y) (+ x y))))
          (foo 6)                                ==>  10

     <Formals> should have one of the following forms:

       * (<variable1> ...,): The procedure takes a fixed number of arguments; when the procedure is called, the arguments will be stored in the bindings of the corresponding variables.

       * <variable>: The procedure takes any number of arguments; when the procedure is called, the sequence of actual arguments is converted into a newly allocated list, and the list is stored in the binding of the <variable>.

       * (<variable1> ..., <variable_n-1> . <variable_n>): If a space-delimited period precedes the last variable, then the value stored in the binding of the last variable will be a newly allocated list of the actual arguments left over after all the other actual arguments have been matched up against the other formal arguments.

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

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 see 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 type gives rewrite rules that will convert constructs described in this section into the primitive constructs described in the previous section.

Conditionals
------------

 -- essential syntax: cond <clause1> <clause2> ...,

     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 <expression1> <expression2> ...,).

     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 see 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

 -- syntax: case <key> <clause1> <clause2> ...,

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

          ((<datum1> ...,) <expression1> <expression2> ...,),

     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 <expression1> <expression2> ...,).

     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 see 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

 -- syntax: and <test1> ...,

     The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see section see 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

 -- syntax: or <test1> ...,

     The <test> expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see section see 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, 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

          ((<variable1> <init1>) ...,),

     where each <init> is an expression, and <body> should be a sequence of one or more expressions.

     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 ((foo (lambda (z) (+ x y z)))
                  (x 7))
              (foo 4)))                          ==>  9

     See also named let, section Iteration.

 -- syntax: let* <bindings> <body>

     Syntax: <Bindings> should have the form

          ((<variable1> <init1>) ...,),

     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 1) (y (+ x 1)))
            y)                                   ==>  2

 -- essential syntax: letrec <bindings> <body>

     Syntax: <Bindings> should have the form

          ((<variable1> <init1>) ...,),

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

     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 ((x 2) (y 3))

          (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 referring to the value of any <variable>.  If this restriction is violated, then the effect is undefined, and an error may be signalled during evaluation of the <init>s.  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 <expression1> <expression2> ...,

     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.

          (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 ((<variable1> <init1> <step1>)

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

     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 see Booleans), then the <command> 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.

     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 see Control features) for a complete description of delay.

Quasiquotation
--------------

 -- syntax: quasiquote <template>

 -- 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 chapter 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 are 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:

  * (define <variable> <expression>)

     This syntax is essential.

  * (define (<variable> <formals>) <body>)

     This syntax is not essential.  <Formals> should be either a sequence of zero or more variables, or a sequence of one or more variables followed by a space-delimited period and another variable (as in a lambda expression).  This form is equivalent to

          (define <variable>
            (lambda (<formals>) <body>)).

  * (define (<variable> . <formal>) <body>)

     This syntax is not essential.  <Formal> should be a single variable.  This form is equivalent to

          (define <variable>
            (lambda <formal> <body>)).

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))))

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 and the empty list count as false in conditional expressions.  Everything else, including #t, pairs, symbols, numbers, strings, vectors, and procedures, counts as true.

The empty list counts as false for compatibility with existing programs and implementations that assume this to be the case.

Programmers accustomed to other dialects of Lisp should beware that Scheme distinguishes false 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 '())                              ==>  #t
          (not (list))                           ==>  #t

 -- essential procedure: boolean? obj

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

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

 -- variable: nil
 -- variable: t

     Some implementations provide variables nil and t whose values in the initial environment are #f and #t respectively.

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

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?.

Two objects are operationally equivalent if and only if there is no way that they can be distinguished, using Scheme primitives other than eqv? or eq? or those like memq and assv whose meaning is defined explicitly in terms of eqv? or eq?.  It is guaranteed that objects maintain their operational identity despite being named by variables or fetched from or stored into data structures.

This definition can be interpreted in the following ways for various kinds of objects:

  * The two boolean values, #t and #f, are operationally distinct because they behave in opposite ways in conditionals.

  * Two symbols are operationally equivalent if they print the same way.

  * Two numbers are operationally equivalent if they are numerically equal (see =, section see Numerical operations) and are either both exact or both inexact (see section see Exactness).

  * Two characters are operationally equivalent if they are the same character according to char=? (section see Characters).

  * Two structured mutable objects---pairs, vectors, or strings---are operationally equivalent if they have operationally equivalent values in corresponding positions, and applying a mutation procedure to one causes the other to change as well.  (A mutation procedure is one like set-car! which alters a data structure.) For example, two pairs are not operationally equivalent if a set-car! operation on one does not change the car field of the other.

  * There is only one empty list, and it is operationally equivalent to itself.  All empty vectors are operationally equivalent to each other.  All empty strings are operationally equivalent to each other.  Whether there is more than one empty vector or string is implementation-dependent.

  * Two procedures are operationally equivalent if, when called on operationally equivalent arguments, they return the same value and perform the same side effects.

 -- essential procedure: eqv? obj1 obj2

     The eqv? procedure implements an approximation to the relation of operational equivalence. It returns #t if it can prove that OBJ1 and OBJ2 are operationally equivalent.  If it can't, it always errs on the conservative side and returns #f.

     The only situation in which it might fail to prove is when OBJ1 and OBJ2 are operationally equivalent procedures that were created at different times.  In general, operational equivalence of procedures is uncomputable, but it is guaranteed that eqv? can recognize a procedure created at a given time by a given lambda expression as ``being itself.''  This is useful for applications in which procedures are being used to implement objects with local state.

          (eqv? 'a 'a)                           ==>  #t
          (eqv? 'a 'b)                           ==>  #f
          (eqv? 2 2)                             ==>  #t
          (eqv? '() '())                         ==>  #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 eqv? is permitted to fail to prove operational equivalence, depending on the implementation.  (In every case, it will return either #t or #f, but which one it returns is implementation-dependent.) Compare with the last example in the previous set.

          (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

     Objects of distinct types are never operationally equivalent, except that false and the empty list are permitted to be identical, and the character type need not be disjoint from other types.

          (eqv? '() #f)                          ==>  unspecified
          (eqv? 57 #\A)                          ==>  unspecified

     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.

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

          Note: The above definition of eqv? allows implementations latitude in their treatment of procedures and literals:  implementations are free to either detect or fail to detect that two procedures or two literals are operationally 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

          Note: 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?. quotation

 -- 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.  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.

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 both representations of the same 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)
     (set-cdr! x 4)                         ==>  unspecified
     x                                      ==>  (a . 4)
     (eqv? x y)                             ==>  #t
     y                                      ==>  (a . 4)

It is often convenient to speak of a homogeneous list of objects of some particular data type, as for example (1 2 3) is a list of integers.  To be more precise, suppose D is some data type.  (Any predicate defines a data type consisting of those objects of which the predicate is true.)  Then

  * The empty list is a list of D.

  * If LIST is a list of D, then any pair whose cdr is LIST and whose car is an element of the data type D is also a list of D.

  * There are no other lists of D.

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 see External representation). 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.

 -- 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.  (In implementations in which the empty list is the same as #f, null? will return #t if OBJ is #f.)

 -- essential procedure: list OBJ ...,

     Returns a 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 list1 list2
 -- 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 it is not a proper list.

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

 -- 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)))))

 -- 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

 -- procedure: last-pair list

     Returns the last pair in the nonempty, possibly improper, list LIST. Last-pair could be defined by

          (define last-pair
            (lambda (x)
              (if (pair? (cdr x))
                  (last-pair (cdr x))
                  x)))

 -- 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.  If OBJ does not occur in LIST, #f (n.b.: 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, #f 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)

          Note: Although they are ordinarily used as predicates, memq, memv, member, assq, assv, and assoc 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 sections Identifiers and 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

 -- 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 see 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 has been no carefully thought out strategy for organizing numerical computation, and with the exception of the MacLisp system [Pitman83] there has been little effort to execute numerical code efficiently.  We applaud the excellent work of the Common Lisp committee and we accept many of their recommendations. In some ways we simplify and generalize their proposals in a manner consistent with the purposes of Scheme.

Scheme's numerical operations treat numbers as abstract data, as independent of their representation as is possible.  Thus, the casual user should be able to write simple programs without having to know that the implementation may use fixed-point, floating-point, and perhaps other representations for his data.  Unfortunately, this illusion of uniformity can be sustained only approximately---the implementation of numbers will leak out of its abstraction whenever the user must be in control of precision, or accuracy, or when he must construct especially efficient computations.  Thus the language must also provide escape mechanisms so that a sophisticated programmer can exercise more control over the execution of his code and the representation of his data when necessary.

It is important to distinguish between the abstract numbers, their machine representations, and their written representations.  We will use mathematical terms number, complex, real, rational, and integer for properties of the abstract numbers, the names fixnum, bignum, ratnum, and flonum for machine representations, and the names int, fix, flo, sci, rat, polar, and rect for input/output formats.

Numbers
-------

A Scheme system provides data of type number, which is the most general numerical type supported by that system.  Number is likely to be a complicated union type implemented in terms of fixnums, bignums, flonums, and so forth, but this should not be apparent to a naive user.  What the user should see is that the usual operations on numbers produce the mathematically expected results, within the limits of the implementation.  Thus if the user divides the exact number 3 by the exact number 2, he should get something like 1.5 (or the exact fraction 3/2).  If he adds that result to itself, and the implementation is good enough, he should get an exact 3.

Mathematically, numbers may be arranged into a tower of subtypes with projections and injections relating adjacent levels of the tower:

begin-tabular

     number

     complex

     real

     rational

     integer

We impose a uniform rule of downward coercion---a number of one type is also of a lower type if the injection (up) of the projection (down) of a number leaves the number unchanged.  Since this tower is a genuine mathematical structure, Scheme provides predicates and procedures to access the tower.

Not all implementations of Scheme must provide the whole tower, but they must implement a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language.

Exactness
---------

Numbers are either exact or inexact.  A number is exact if it was derived from exact numbers using only exact operations.  A number is inexact if it models a quantity (e.g., a measurement) known only approximately, if it was derived using inexact ingredients, or if it was derived using inexact operations. Thus inexactness is a contagious property of a number.  Some operations, such as the square root (of non-square numbers), must be inexact because of the finite precision of our representations.

Other operations are inexact because of implementation requirements.  We emphasize that exactness is independent of the position of the number on the tower.  It is perfectly possible to have an inexact integer or an exact real; 355/113 may be an exact rational or it may be an inexact rational approximation to pi, depending on the application.

Operationally, it is the system's responsibility to combine exact numbers using exact methods, such as infinite precision integer and rational arithmetic, where possible.  An implementation may not be able to do this (if it does not use infinite precision integers and rationals), but if a number becomes inexact for implementation reasons there is likely to be an important error condition, such as integer overflow, to be reported.  Arithmetic on inexact numbers is not so constrained.  The system may use floating point and other ill-behaved representation strategies for inexact numbers.  This is not to say that implementors need not use the best known algorithms for inexact computations---only that approximate methods of high quality are allowed. In a system that cannot explicitly distinguish exact from inexact numbers the system must do its best to maintain precision.  Scheme systems must not burden users with numerical operations described in terms of hardware and operating-system dependent representations such as fixnum and flonum, however, because these representation issues are hardly ever germane to the user's problems.

We highly recommend that the IEEE 32-bit and 64-bit floating-point standards be adopted for implementations that use floating-point representations internally.  To minimize loss of precision we adopt the following rules: If an implementation uses several different sizes of floating-point formats, the results of any operation with a floating-point result must be expressed in the largest format used to express any of the floating-point arguments to that operation.  It is desirable (but not required) for potentially irrational 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 an exact number (or an inexact number represented as a fixnum, a bignum, or a ratnum) is operated upon so as to produce an inexact result (as by sqrt), and if the result is represented as a flonum, then the largest available flonum format must be used; but if the result is expressed as a ratnum then the rational approximation must have at least as much precision as the largest available flonum.

Number syntax
-------------

Scheme allows the traditional ways of writing numerical constants, though any particular implementation may support only some of them. These syntaxes are intended to be purely notational; any kind of number may be written in any form that the user deems convenient.  Of course, writing 1/7 as a limited-precision decimal fraction will not express the number exactly, but this approximate form of expression may be just what the user wants to see.

The syntax of numbers is described formally in section Lexical structure.  See section Formats for many examples of representations of numbers.

A numerical constant may be represented in binary, octal, decimal, or hex by the use of a radix prefix.  The radix prefixes are #b (binary), #o (octal), #d (decimal), and #x (hex).  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 representation of a numerical constant has no exactness prefix, the constant may be assumed to be exact or inexact at the discretion of the implementation, except that integers expressed without decimal points and without use of exponential notation are assumed exact.

In systems with both single and double precision flonums we may want to specify which size we want to use to represent a constant internally.  For example, we may want a constant that has the value of pi rounded to the single precision length, or we might want a long number that has the value 6/10.  In either case, we are specifying an explicit way to represent an inexact number.  For this purpose, we may express a number with a prefix that indicates short or long flonum representation:

     #S3.14159265358979
            Round to short --- 3.141593
     #L.6
            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.

 -- 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 true if the object is of the named type.  In general, if a type predicate is true of a number then all higher type predicates are also true of that number.  Not every system supports all of these types; for example, it is entirely possible to have a Scheme system that has only integers.  Nonetheless every implementation of Scheme must have all of these predicates.

 -- essential procedure: zero? Z
 -- essential procedure: positive? X
 -- essential procedure: negative? X
 -- essential procedure: odd? N
 -- essential procedure: even? N
 -- essential procedure: exact? Z
 -- essential procedure: inexact? Z

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

 -- essential procedure: = z1 z2
 -- essential procedure: < x1 x2
 -- essential procedure: > x1 x2
 -- essential procedure: <= x1 x2
 -- essential procedure: >= x1 x2

     Some implementations allow these procedures to take many arguments, to facilitate range checks.  These procedures return #t if their arguments are (respectively): numerically equal, monotonically increasing, monotonically decreasing, monotonically nondecreasing, or monotonically nonincreasing.  Warning: on inexact numbers the equality tests will give unreliable results, and the other numerical comparisons will be useful only heuristically; when in doubt, consult a numerical analyst.

 -- essential procedure: max x1 x2
 -- procedure: max x1 x2 ...,
 -- essential procedure: min x1 x2
 -- procedure: min x1 x2 ...,

     These procedures return the maximum or minimum of their arguments.

 -- essential procedure: + z1 z2
 -- procedure: + z1 ...,
 -- essential procedure: * z1 z2
 -- 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
 -- procedure: - z1 z2 ...,
 -- essential procedure: / z1 z2
 -- 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 z

     Abs returns the magnitude of its argument.

          (abs -7)                               ==>  7
          (abs -3+4i)                            ==>  5

 -- essential procedure: quotient n1 n2
 -- essential procedure: remainder n1 n2
 -- procedure: modulo n1 n2

     These are intended to implement number-theoretic (integer) division: For positive integers N1 and N2, if N3 and N4 are integers such that N1=N2N3+N4 and 0<= N4<N2, then

          (quotient N1 N2)                       ==>  N3
          (remainder N1 N2)                      ==>  N4
          (modulo N1 N2)                         ==>  N4

     For all integers N1 and N2 with N2 not equal to 0,

          (= N1 (+ (* N2 (quotient N1 N2))
                        (remainder N1 N2)))
                                                 ==>  #t

     The value returned by quotient always has the sign of the product of its arguments.  Remainder and modulo differ on negative arguments---the remainder always has the sign of the dividend, 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

 -- procedure: numerator Q
 -- procedure: denominator Q

     These procedures return the numerator or denominator of their argument.

          (numerator (/ 6 4))                    ==>  3
          (denominator (/ 6 4))                  ==>  2

 -- procedure: gcd n1 ...,
 -- 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)                                  ==>  1

 -- procedure: floor x
 -- procedure: ceiling x
 -- procedure: truncate x
 -- procedure: round x
 -- procedure: rationalize x y
 -- procedure: rationalize x

     These procedures create integers and rationals.  Their results are exact if and only if their arguments are exact.

     Floor returns the largest integer not larger than X. Ceiling returns the smallest integer not smaller than X. Truncate returns the integer of maximal absolute value 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.  With two arguments, rationalize produces the rational number with smallest denominator differing from X by no more than Y.  With one argument, rationalize produces the best rational approximation to X, preserving all of the precision in its representation.

          Note: Round rounds to even for consistency with the rounding modes required by the IEEE floating point standard.

 -- 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 real numbers; they compute the usual transcendental functions.  Log computes the natural logarithm of Z (not the base 10 logarithm). Asin, acos, and atan compute arcsine (sin^-1), arccosine (cos^-1), and arctangent (tan^-1), respectively. The two-argument variant of atan computes (angle (make-rectangular X Y)) (see below), even in implementations that don't support 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

     log z = log magnitude(z) + i --> angle (z)

     With log defined this way, the values of sin^-1 z, cos^-1 z, and tan^-1 z are according to the following formulae:

     sin^-1 z = -i log (i z + sqrt1 - z^2)

     cos^-1 z = -i log (z + i sqrt1 - z^2)

     tan^-1 z = -i log ( (1 + i z) sqrt1/(1 + z^2) )

     The above specification follows [CLtL], which in turn follows [Penfield81]; refer to these sources for more detailed discussion of branch cuts, boundary conditions, and implementation of these functions.

 -- 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:

     z_1^z_2 = e^z_2 log z_1

     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 complex numbers.  Suppose X1, X2, X3, and X4 are real numbers and Z is a complex number such that

     Z = X1 + X2i = X3 . e^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).

          Note: Magnitude is the same as abs, but abs must be present in all implementations, whereas magnitude will only be present in implementations that support complex numbers.

 -- procedure: exact->inexact Z
 -- procedure: inexact->exact Z

     Exact->inexact returns an inexact representation of Z, which is a fairly harmless thing to do.  Inexact->exact returns an exact representation of Z.  Since the law of ``garbage in, garbage out'' remains in force, inexact->exact should not be used casually.

Numerical input and output
--------------------------

 -- procedure: number->string number format

     The conventions used to produce the printed representation of a number can be specified by a format, as described in section Formats. The procedure number->string takes a number and a format and returns as a string the printed representation of the given number in the given format.  This procedure will mostly be used by sophisticated users and in system programs.  In general, a naive user will need to know nothing about the formats because the system printer will have reasonable default formats for all types of numbers.

 -- procedure: string->number string exactness radix

     The system reader will construct reasonable default numerical types for numbers expressed in each of the formats it recognizes.  A user who needs control of the coercion from strings to numbers will use string->number. EXACTNESS must be a symbol, either E (for exact) or I (for inexact).  RADIX must also be a symbol: B for binary, O for octal, D for decimal, and X for hexadecimal.  Returns a number of the maximally precise representation expressed by the given STRING.  It is an error if STRING does not express a number according to the grammar in section Lexical structure.

Formats
-------

A format is a list beginning with a format descriptor, which is a symbol such as sci.  Following the descriptor are parameters used by that descriptor, such as the number of significant digits to be used.  Default values are supplied for any parameters that are omitted. Modifiers may appear after the parameters, such as the radix and exactness formats described below, which themselves take parameters.

Details of particular formats such as sci and fix are given in section Details of formats.

For example, the format (sci 5 2 (exactness s)) specifies that a number is to be expressed in Fortran scientific format with 5 significant places and two places after the radix point, and that its exactness prefix is to be suppressed.

In the following examples, the comment shows the format that was used to produce the output shown:

     123  +123  -123              ; (int)
     123456789012345678901234567  ; (int)
     355/113  +355/113  -355/113  ; (rat)
     +123.45  -123.45             ; (fix 2)
     3.14159265358979             ; (fix 14)
     3.14159265358979             ; (flo 15)
     123.450                      ; (flo 6)
     -123.45e-1                   ; (sci 5 2)
     123e3  123e-3  -123e-3       ; (sci 3 0)
     -1+2i                        ; (rect (int) (int))
     1.2@1.570796                 ; (polar (fix 1)
                                  ;        (flo 7))

A format may specify that a number should be expressed in a particular radix.  The radix prefix may also be suppressed.  For example, one may express a complex number in polar form with the magnitude in octal and the angle in decimal as follows:

     #o1.2@#d1.570796327 ; (polar (flo 2 (radix o))
                         ;        (flo (radix d)))
     #o1.2@1.570796327   ; (polar (flo 2 (radix o))
                         ;        (flo (radix d s)))

A format may specify that a number should be expressed with an explicit exactness prefix ((exactness e)), or it may force the exactness to be suppressed ((exactness s)).  For example, the following are ways to express an inexact value for pi:

     #i355/113           ; (rat (exactness e))
     355/113             ; (rat (exactness s))
     #i3.1416            ; (fix 4 (exactness e))

An attempt to produce more digits than are available in the internal machine representation of a number will be marked with a ``#'' filling the extra digits.  This is not a statement that the implementation knows or keeps track of the significance of a number, just that the machine will flag attempts to produce 20 digits of a number that has only 15 digits of machine representation:

     3.14158265358979#####   ; (flo 20 (exactness s))

Details of formats
------------------

The format descriptors are:

 -- format: int

     Express as an integer.  The radix point is implicit.  If there are not enough significant places then insignificant digits will be flagged.  For example, an inexact integer 6.0238.10^23 (represented internally as a 7 digit flonum) would be printed as

          6023800#################

 -- format: rat N

     Express as a rational fraction.  N specifies the largest denominator to be used in constructing a rational approximation to the number being expressed.  If N is omitted it defaults to infinity.

 -- format: fix N

     Express with a fixed radix point.  N specifies the number of places to the right of the radix point.  N defaults to the size of a single-precision flonum.  If there are not enough significant places, then insignificant digits will be flagged.  For example, an inexact 6.0238.10^23 (represented internally as a 7 digit flonum) would be printed with a (fix 2) format as

          6023800#################.##

 -- format: flo N

     Express with a floating radix point.  N specifies the total number of places to be displayed.  N defaults to the size of a single-precision flonum.  If the number is out of range, it is converted to (sci).  (flo h) expresses N in floating point format heuristically for human consumption.

 -- format: sci N M

     Express in exponential notation.  N specifies the total number of places to be displayed.  N defaults to the size of a single-precision flonum.  M specifies the number of places to the right of the radix point.  M defaults to N-1.  (sci h) does heuristic expression.

 -- format: rect R I

     Express as a rectangular form complex number.  R and I are formats for the real and imaginary parts respectively.  They default to (heur).

 -- format: polar M A

     Express as a polar form complex number.  M and A are formats for the magnitude and angle respectively.  M and A default to (heur).

 -- format: heur

     Express heuristically using the minimum number of digits required to get an expression that when coerced back to a number produces the original machine representation.  Exact numbers are expressed as (int) or (rat).  Inexact numbers are expressed as (flo h) or (sci h) depending on their range.  Complex numbers are expressed in (rect).  This is the normal default of the system printer.

The following modifiers may be added to a numerical format specification:

 -- format: exactness S

     This controls the expression of the exactness prefix of a number.  S must be a symbol, either E or S, indicating whether the exactness is to be expressed or suppressed, respectively.  If no exactness modifier is specified for a format then the exactness is by default suppressed.

 -- format: radix R S

     This forces a number to be expressed in the radix R.  R may be the symbol B (binary), O (octal), D (decimal), or X (hex). S must be a symbol, either E or S, indicating whether the radix prefix is to be expressed or suppressed, respectively. S defaults to E (expressed).  If no radix modifier is specified then the default is decimal and the prefix is suppressed.

Characters
==========

Characters are objects that represent printed characters such as letters and digits.  There is no requirement that the data type of characters be disjoint from other data types; implementations are encouraged to have a separate character data type, but may choose to represent characters as integers, strings, or some other type.

Characters are written using the notation #\<character> or #\<character name>. For example:

begin-tabular

     #\a
          ; lower case letter

     #\A
          ; upper case letter

     #\(
          ; left parenthesis

     #\
          ; the space character

     #\space
          ; the preferred way to write a space

     #\newline
          ; the newline character

Case is significant in #\<character>, but not in #\<character name>.  If <character> in #\<character> is alphabetic, then the character following <character> must be a delimiter character such as a space or parenthesis.  This rule resolves the ambiguous case where, for example, the sequence of characters ``#\ space'' could be taken to be either a representation of the space character or a representation of the character ``#\ s'' followed by a representation of the symbol ``pace.''

Characters written in the #\ notation are self-evaluating. That is, they do not have to be quoted in programs.  The #\ notation is not an essential part of Scheme, however.  Even implementations that support the #\ notation for input do not have to support it for output.

Some of the procedures that operate on characters ignore the difference between upper case and lower case.  The procedures that ignore case have the suffix ``-ci'' (for ``case insensitive'').  If the operation is a predicate, then the ``-ci'' suffix precedes the ``?'' at the end of the name.

 -- essential procedure: char? obj

     Returns #t if OBJ is a character, otherwise returns #f.

 -- essential procedure: char=? char1 char2
 -- essential procedure: char<? char1 char2
 -- essential procedure: char>? char1 char2
 -- essential procedure: char<=? char1 char2
 -- essential procedure: char>=? char1 char2

     These procedures impose a total ordering on the set of characters.  It is guaranteed that under this ordering:

       * The upper case characters are in order.  For example, (char<? #\A #\B) returns #t.

       * The lower case characters are in order.  For example, (char<? #\a #\b) returns #t.

       * The digits are in order.  For example, (char<? #\0 #\9) returns #t.

       * Either all the digits precede all the upper case letters, or vice versa.

       * Either all the digits precede all the lower case letters, or vice versa.

     Some implementations may generalize these procedures to take more than two arguments, as with the corresponding numeric predicates.

 -- procedure: char-ci=? char1 char2
 -- procedure: char-ci<? char1 char2
 -- procedure: char-ci>? char1 char2
 -- procedure: char-ci<=? char1 char2
 -- procedure: char-ci>=? char1 char2

     These procedures are similar to char=? et cetera, but they treat upper case and lower case letters as the same.  For example, (char-ci=? #\A #\a) returns #t.  Some implementations may generalize these procedures to take more than two arguments, as with the corresponding numeric predicates.

 -- procedure: char-alphabetic? char
 -- procedure: char-numeric? char
 -- procedure: char-whitespace? char

     These procedures return #t if their arguments are alphabetic, numeric, or whitespace characters, respectively, otherwise they return #f.  The following remarks, which are specific to the ASCII character set, are intended only as a guide:  The alphabetic characters are the 52 upper and lower case letters.  The numeric characters are the ten decimal digits.  The whitespace characters are space, tab, line feed, form feed, and carriage return.

 -- procedure: char-upper-case? letter
 -- procedure: char-lower-case? letter

     LETTER must be an alphabetic character. These procedures return #t if their arguments are upper case or lower case characters, respectively, otherwise they return #f.

 -- essential procedure: char->integer char
 -- essential procedure: integer->char N

     Given a character, char->integer returns an integer representation of the character.  Given an integer that is the image of a character under char->integer, integer->char returns a character.  These procedures implement order isomorphisms between the set of characters under the char<=? ordering and some subset of the integers under the <= ordering.  That is, if

          (char<=? A B) => #t  and  (<= X Y) => #t

     and X and Y are in the range of char->integer, then

          (<= (char->integer A)
              (char->integer B))                 ==>  #t

          (char<=? (integer->char X)
                   (integer->char Y))            ==>  #t

 -- procedure: char-upcase char
 -- procedure: char-downcase char

     These procedures return a character CHAR2 such that (char-ci=? CHAR CHAR2).  In addition, if CHAR is alphabetic, then the result of char-upcase is upper case and the result of char-downcase is lower case.

Strings
=======

Strings are sequences of characters.  In some implementations of Scheme they are immutable; other implementations provide destructive procedures such as string-set! that alter string objects.

Strings are written as sequences of characters enclosed within doublequotes (").  A doublequote can be written inside a string only by escaping it with a backslash (\), as in

     "The word \"recursion\" has many meanings."

A backslash can be written inside a string only by escaping it with another backslash.  Scheme does not specify the effect of a backslash within a string that is not followed by a doublequote or backslash.

A string may continue from one line to the next, but this is usually a bad idea because the exact effect may vary from one computer system to another.

The length of a string is the number of characters that it contains.  This number is a non-negative integer that is fixed when the string is created.  The valid indexes of a string are the exact non-negative integers less than the length of the string.  The first character of a string has index 0, the second has index 1, and so on.

In phrases such as ``the characters of STRING beginning with index START and ending with index END,'' it is understood that the index START is inclusive and the index END is exclusive.  Thus if START and END are the same index, a null substring is referred to, and if START is zero and END is the length of STRING, then the entire string is referred to.

Some of the procedures that operate on strings ignore the difference between upper and lower case.  The versions that ignore case have the suffix ``-ci'' (for ``case insensitive'').  If the operation is a predicate, then the ``-ci'' suffix precedes the ``?'' at the end of the name.

 -- essential procedure: string? obj

     Returns #t if OBJ is a string, otherwise returns #f.

 -- procedure: make-string K
 -- procedure: make-string K char

     K must be a non-negative integer, and CHAR must be a character.  Make-string returns a newly allocated string of length K.  If CHAR is given, then all elements of the string are initialized to CHAR, otherwise the contents of the STRING are unspecified.

 -- essential procedure: string-length string

     Returns the number of characters in the given STRING.

 -- essential procedure: string-ref string K

     K must be a valid index of STRING. String-ref returns character K of STRING using zero-origin indexing.

 -- procedure: string-set! string k char

     K must be a valid index of STRING . String-set! stores CHAR in element K of STRING and returns an unspecified value.

 -- essential procedure: string=? string1 string2
 -- procedure: string-ci=? string1 string2

     Returns #t if the two strings are the same length and contain the same characters in the same positions, otherwise returns #f. String-ci=? treats upper and lower case letters as though they were the same character, but string=? treats upper and lower case as distinct characters.

 -- essential procedure: string<? string1 string2
 -- essential procedure: string>? string1 string2
 -- essential procedure: string<=? string1 string2
 -- essential procedure: string>=? string1 string2
 -- procedure: string-ci<? string1 string2
 -- procedure: string-ci>? string1 string2
 -- procedure: string-ci<=? string1 string2
 -- procedure: string-ci>=? string1 string2

     These procedures are the lexicographic extensions to strings of the corresponding orderings on characters.  For example, string<? is the lexicographic ordering on strings induced by the ordering char<? on characters.  If two strings differ in length but are the same up to the length of the shorter string, the shorter string is considered to be lexicographically less than the longer string.

     Implementations may generalize these and the string=? and string-ci=? procedures to take more than two arguments, as with the corresponding numeric predicates.

 -- essential procedure: substring string start end

     STRING must be a string, and START and END must be exact integers satisfying

     0 <= START <= END <= (string-length STRING).

     Substring returns a newly allocated string formed from the characters of STRING beginning with index START (inclusive) and ending with index END (exclusive).

 -- essential procedure: string-append string1 string2
 -- procedure: string-append STRING ...,

     Returns a new string whose characters form the concatenation of the given strings.

 -- essential procedure: string->list string
 -- essential procedure: list->string chars

     String->list returns a newly allocated list of the characters that make up the given string.  List->string returns a string formed from the characters in the list CHARS. String->list and list->string are inverses so far as equal? is concerned.  Implementations that provide destructive operations on strings should ensure that the result of list->string is newly allocated.

 -- procedure: string-copy string

     Returns a newly allocated copy of the given STRING.

 -- procedure: string-fill! string char

     Stores CHAR in every element of the given STRING and returns an unspecified value.

Vectors
=======

Vectors are heterogenous mutable structures whose elements are indexed by integers.

The length of a vector is the number of elements that it contains.  This number is a non-negative integer that is fixed when the vector is created.  The valid indexes of a vector are the exact non-negative integers less than the length of the vector.  The first element in a vector is indexed by zero, and the last element is indexed by one less than the length of the vector.

Vectors are written using the notation #(OBJ ...,). For example, a vector of length 3 containing the number zero in element 0, the list (2 2 2 2) in element 1, and the string "Anna" in element 2 can be written as following:

     #(0 (2 2 2 2) "Anna")

Note that this is the external representation of a vector, not an expression evaluating to a vector.  Like list constants, vector constants must be quoted:

     '#(0 (2 2 2 2) "Anna")
               ==>  #(0 (2 2 2 2) "Anna")

 -- essential procedure: vector? obj

     Returns #t if OBJ is a vector, otherwise returns #f.

 -- essential procedure: make-vector k
 -- procedure: make-vector k fill

     Returns a newly allocated vector of K elements.  If a second argument is given, then each element is initialized to FILL. Otherwise the initial contents of each element is unspecified.

 -- essential procedure: vector obj ...,

     Returns a newly allocated vector whose elements contain the given arguments.  Analogous to list.

          (vector 'a 'b 'c)                      ==>  #(a b c)

 -- essential procedure: vector-length vector

     Returns the number of elements in VECTOR.

 -- essential procedure: vector-ref vector k

     K must be a valid index of VECTOR. Vector-ref returns the contents of element K of VECTOR.

          (vector-ref '#(1 1 2 3 5 8 13 21) 5)  =>  8

 -- essential procedure: vector-set! vector k obj

     K must be a valid index of VECTOR. Vector-set! stores OBJ in element K of VECTOR. The value returned by vector-set! is unspecified.

          (let ((vec (vector 0 '(2 2 2 2) "Anna")))
            (vector-set! vec 1 '("Sue" "Sue"))
            vec)
                    ==>  #(0 ("Sue" "Sue") "Anna")

 -- essential procedure: vector->list vector
 -- essential procedure: list->vector list

     Vector->list returns a newly created list of the objects contained in the elements of VECTOR.  List->vector returns a newly created vector initialized to the elements of the list LIST.

          (vector->list '#(dah dah didah))
                    ==>  (dah dah didah)
          (list->vector '(dididit dah))
                    ==>  #(dididit dah)

 -- procedure: vector-fill! vector fill

     Stores FILL in every element of VECTOR. The value returned by vector-fill! is unspecified.

Control features
================

This chapter describes various primitive procedures which control the flow of program execution in special ways. The procedure? predicate is also described here.

 -- essential procedure: procedure? obj

     Returns #t if OBJ is a procedure, otherwise returns #f.

          (procedure? car)                       ==>  #t
          (procedure? 'car)                      ==>  #f
          (procedure? (lambda (x) (* x x)))
                                                 ==>  #t
          (procedure? '(lambda (x) (* x x)))
                                                 ==>  #f
          (call-with-current-continuation procedure?)
                                                 ==>  #t

 -- essential procedure: apply proc args
 -- procedure: apply proc arg1 ... args

     PROC must be a procedure and ARGS must be a list. The first (essential) form calls PROC with the elements of ARGS as the actual arguments.  The second form is a generalization of the first that calls PROC with the elements of the list (append (list ARG1 ...,) ARGS) as the actual arguments.

          (apply + (list 3 4))                   ==>  7

          (define compose
            (lambda (f g)
              (lambda args
                (f (apply g args)))))

          ((compose sqrt *) 12 75)               ==>  30

 -- essential procedure: map proc list
 -- procedure: map proc list1 list2 ...,

     The LISTs must be lists, and PROC must be a procedure taking as many arguments as there are lists.  If more than one LIST is given, then they must all be the same length. Map applies PROC element-wise to the elements of the LISTs and returns a list of the results.  The order in which PROC is applied to the elements of the LISTs is unspecified.

          (map cadr '((a b) (d e) (g h)))
                    ==>  (b e h)

          (map (lambda (n) (expt n n))
               '(1 2 3 4 5))
                    ==>  (1 4 27 256 3125)

          (map + '(1 2 3) '(4 5 6))              ==>  (5 7 9)

          (let ((count 0))
            (map (lambda (ignored)
                   (set! count (+ count 1))
                   count)
                 '(a b c)))                      ==>  unspecified

 -- essential procedure: for-each proc list
 -- procedure: for-each proc list1 list2 ...,

     The arguments to for-each are like the arguments to map, but for-each calls PROC for its side effects rather than for its values.  Unlike map, for-each is guaranteed to call PROC on the elements of the LISTs in order from the first element to the last, and the value returned by for-each is unspecified.

          (let ((v (make-vector 5)))
            (for-each (lambda (i)
                        (vector-set! v i (* i i)))
                      '(0 1 2 3 4))
            v)                                   ==>  #(0 1 4 9 16)

 -- procedure: force promise

     Forces the value of PROMISE (see delay, section see Delayed evaluation).  If no value has been computed for the promise, then a value is computed and returned.  The value of the promise is cached (or ``memoized'') so that if it is forced a second time, the previously computed value is returned without any recomputation.

          (force (delay (+ 1 2)))                ==>  3
          (let ((p (delay (+ 1 2))))
            (list (force p) (force p)))
                                                 ==>  (3 3)

          (define a-stream
            (letrec ((next
                      (lambda (n)
                        (cons n (delay (next (+ n 1)))))))
              (next 0)))
          (define head car)
          (define tail
            (lambda (stream) (force (cdr stream))))

          (head (tail (tail a-stream)))
                                                 ==>  2

     Force and delay are mainly intended for programs written in functional style.  The following examples should not be considered to illustrate good programming style, but they illustrate the property that the value of a promise is computed at most once.

          (define count 0)
          (define p (delay (begin (set! count (+ count 1))
                                  (* x 3))))
          (define x 5)
          count                                  ==>  0
          p                                      ==>  a promise
          (force p)                              ==>  15
          p                                      ==>  a promise, still
          count                                  ==>  1
          (force p)                              ==>  15
          count                                  ==>  1

     Here is a possible implementation of delay and force.  We define the expression

          (delay <expression>)

     to have the same meaning as the procedure call

          (make-promise (lambda () <expression>)),

     where make-promise is defined as follows:

          (define make-promise
            (lambda (proc)
              (let ((already-run? #f) (result #f))
                (lambda ()
                  (cond ((not already-run?)
                         (set! result (proc))
                         (set! already-run? #t)))
                  result))))

     Promises are implemented here as procedures of no arguments, and force simply calls its argument.

          (define force
            (lambda (object)
              (object)))

     Various extensions to this semantics of delay and force are supported in some implementations:

       * Calling force on an object that is not a promise may simply return the object.

       * It may be the case that there is no means by which a promise can be operationally distinguished from its forced value.  That is, expressions like the following may evaluate to either #t or to #f, depending on the implementation:

               (eqv? (delay 1) 1)                ==>  unspecified
               (pair? (delay (cons 1 2)))        ==>  unspecified

       * Some implementations will implement ``implicit forcing,'' where the value of a promise is forced by primitive procedures like cdr and +:

               (+ (delay (* 3 7)) 13)            ==>  34

 -- essential procedure: call-with-current-continuation proc

     PROC must be a procedure of one argument. The procedure call-with-current-continuation packages up the current continuation (see the rationale below) as an ``escape procedure'' and passes it as an argument to PROC.  The escape procedure is a Scheme procedure of one argument that, if it is later passed a value, will ignore whatever continuation is in effect at that later time and will give the value instead to the continuation that was in effect when the escape procedure was created.

     The escape procedure created by call-with-current-continuation has unlimited extent just like any other procedure in Scheme.  It may be stored in variables or data structures and may be called as many times as desired.

     The following examples show only the most common uses of call-with-current-continuation.  If all real programs were as simple as these examples, there would be no need for a procedure with the power of call-with-current-continuation.

          (call-with-current-continuation
            (lambda (exit)
              (for-each (lambda (x)
                          (if (negative? x)
                              (exit x)))
                        '(54 0 37 -3 245 19))
              #t))                               ==>  -3

          (define list-length
            (lambda (obj)
              (call-with-current-continuation
                (lambda (return)
                  (letrec ((r
                            (lambda (obj)
                              (cond ((null? obj) 0)
                                    ((pair? obj)
                                     (+ (r (cdr obj)) 1))
                                    (else (return #f))))))
                    (r obj))))))

          (list-length '(1 2 3 4))               ==>  4

          (list-length '(a b . c))               ==>  #f

          Rationale: A common use of call-with-current-continuation is for structured, non-local exits from loops or procedure bodies, but in fact call-with-current-continuation is extremely useful for implementing a wide variety of advanced control structures.

          Whenever a Scheme expression is evaluated there is a continuation wanting the result of the expression.  The continuation represents an entire (default) future for the computation.  If the expression is evaluated at top level, for example, then the continuation will take the result, print it on the screen, prompt for the next input, evaluate it, and so on forever.  Most of the time the continuation includes actions specified by user code, as in a continuation that will take the result, multiply it by the value stored in a local variable, add seven, and give the answer to the top level continuation to be printed.  Normally these ubiquitous continuations are hidden behind the scenes and programmers don't think much about them.  On rare occasions, however, a programmer may need to deal with continuations explicitly. Call-with-current-continuation allows Scheme programmers to do that by creating a procedure that acts just like the current continuation.

          Most programming languages incorporate one or more special-purpose escape constructs with names like exit, return, or even goto.  In 1965, however, Peter Landin [Landin65] invented a general purpose escape operator called the J-operator.  John Reynolds [Reynolds72] described a simpler but equally powerful construct in 1972.  The catch special form described by Sussman and Steele in the 1975 report on Scheme is exactly the same as Reynolds's construct, though its name came from a less general construct in MacLisp.  Several Scheme implementors noticed that the full power of the catch construct could be provided by a procedure instead of by a special syntactic construct, and the name call-with-current-continuation was coined in 1982.  This name is descriptive, but opinions differ on the merits of such a long name, and some people use the name call/cc instead.

Input and output
================

Ports
-----

Ports represent input and output devices.  To Scheme, an input device is a Scheme object that can deliver characters upon command, while an output device is a Scheme object that can accept characters.

 -- essential procedure: call-with-input-file string proc
 -- essential procedure: call-with-output-file string proc

     PROC should be a procedure of one argument, and STRING should be a string naming a file.  For call-with-input-file, the file must already exist; for call-with-output-file, the effect is unspecified if the file already exists. These procedures call PROC with one argument: the port obtained by opening the named file for input or output.  If the file cannot be opened, an error is signalled.  If the procedure returns, then the port is closed automatically and the value yielded by the procedure is returned.  If the procedure does not return, then Scheme will not close the port unless it can prove that the port will never again be used for a read or write operation.

          Rationale: Because Scheme's escape procedures have unlimited extent, it  is possible to escape from the current continuation but later to escape back in. If implementations were permitted to close the port on any escape from the current continuation, then it would be impossible to write portable code using both call-with-current-continuation and call-with-input-file or call-with-output-file.

 -- essential procedure: input-port? obj
 -- essential procedure: output-port? obj

     Returns #t if OBJ is an input port or output port respectively, otherwise returns #f.

 -- essential procedure: current-input-port
 -- essential procedure: current-output-port

     Returns the current default input or output port.

 -- procedure: with-input-from-file string thunk
 -- procedure: with-output-to-file string thunk

     THUNK must be a procedure of no arguments, and STRING must be a string naming a file.  For with-input-from-file, the file must already exist; for with-output-to-file, the effect is unspecified if the file already exists. The file is opened for input or output, an input or output port connected to it is made the default value returned by current-input-port or current-output-port, and the THUNK is called with no arguments.  When the THUNK returns, the port is closed and the previous default is restored. With-input-from-file and with-output-to-file return the value yielded by THUNK. If an escape procedure is used to escape from the continuation of these procedures, their behavior is implementation dependent.

 -- procedure: open-input-file filename

     Takes a string naming an existing file and returns an input port capable of delivering characters from the file.  If the file cannot be opened, an error is signalled.

 -- procedure: open-output-file filename

     Takes a string naming an output file to be created and returns an output port capable of writing characters to a new file by that name.  If the file cannot be opened, an error is signalled.  If a file with the given name already exists, the effect is unspecified.

 -- procedure: close-input-port port
 -- procedure: close-output-port port

     Closes the file associated with PORT, rendering the PORT incapable of delivering or accepting characters.

     These routines have no effect if the file has already been closed. The value returned is unspecified.

Input
-----

 -- essential procedure: read
 -- essential procedure: read port

     Read converts written representations of Scheme objects into the objects themselves.  That is, it is a parser for the nonterminal <datum> (see section see External representation).  Read returns the next object parsable from the given input PORT, updating PORT to point to the first character past the end of the written representation of the object.

     If an end of file is encountered in the input before any characters are found that can begin an object, then an end of file object is returned. The port remains open, and further attempts to read will also return an end of file object.  If an end of file is encountered after the beginning of an object's written representation, but the written representation is incomplete and therefore not parsable, an error is signalled.

     The PORT argument may be omitted, in which case it defaults to the value returned by current-input-port.  It is an error to read from a closed port.

 -- essential procedure: read-char
 -- essential procedure: read-char port

     Returns the next character available from the input PORT, updating the PORT to point to the following character.  If no more characters are available, an end of file object is returned.  PORT may be omitted, in which case it defaults to the value returned by current-input-port.

 -- procedure: char-ready?
 -- procedure: char-ready? port

     Returns #t if a character is ready on the input PORT and returns #f otherwise.  If char-ready returns #t then the next read-char operation on the given PORT is guaranteed not to hang.  If the PORT is at end of file then char-ready? returns #t.  PORT may be omitted, in which case it defaults to the value returned by current-input-port.

          Rationale: Char-ready? exists to make it possible for a program to accept characters from interactive ports without getting stuck waiting for input.  Any input editors associated with such ports must ensure that characters whose existence has been asserted by char-ready? cannot be rubbed out.  If char-ready? were to return #f at end of file, a port at end of file would be indistinguishable from an interactive port that has no ready characters.

 -- essential procedure: eof-object? obj

     Returns #t if OBJ is an end of file object, otherwise returns #f.  The precise set of end of file objects will vary among implementations, but in any case no end of file object will ever be a character or an object that can be read in using read.

Output
------

 -- essential procedure: write obj
 -- essential procedure: write obj port

     Writes a representation of OBJ to the given PORT.  Strings that appear in the written representation are enclosed in doublequotes, and within those strings backslash and doublequote characters are escaped by backslashes.  Write returns an unspecified value.  The PORT argument may be omitted, in which case it defaults to the value returned by current-output-port.

 -- essential procedure: display obj
 -- essential procedure: display obj port

     Writes a representation of OBJ to the given PORT.  Strings that appear in the written representation are not enclosed in doublequotes, and no characters are escaped within those strings.  In those implementations that have a distinct character type, character objects appear in the representation as if written by write-char instead of by write.  Display returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by current-output-port.

          Rationale: Write is intended for producing machine-readable output and display is for producing human-readable output.  Implementations that allow ``slashification'' within symbols will probably want write but not display to slashify funny characters in symbols.

 -- essential procedure: newline
 -- essential procedure: newline port

     Writes an end of line to PORT.  Exactly how this is done differs from one operating system to another.  Returns an unspecified value. The PORT argument may be omitted, in which case it defaults to the value returned by current-output-port.

 -- essential procedure: write-char char
 -- essential procedure: write-char char port

     Writes the character CHAR (not a written representation of the character) to the given PORT and returns an unspecified value.  The PORT argument may be omitted, in which case it defaults to the value returned by current-output-port.

User interface
--------------

Questions of user interface generally fall outside of the domain of this report.  However, the following operations are important enough to deserve description here.

 -- essential procedure: load filename

     FILENAME should be a string naming an existing file containing Scheme source code. The load procedure reads expressions and definitions from the file and evaluates them sequentially.  It is unspecified whether the results of the expressions are printed.  The load procedure does not affect the values returned by current-input-port and current-output-port. Load returns an unspecified value.

          Note: For portability, load must operate on source files. Its operation on other kinds of files necessarily varies among implementations.

 -- procedure: transcript-on filename
 -- procedure: transcript-off

     FILENAME must be a string naming an output file to be created. The effect of transcript-on is to open the named file for output, and to cause a transcript of subsequent interaction between the user and the Scheme system to be written to the file.  The transcript is ended by a call to transcript-off, which closes the transcript file.  Only one transcript may be in progress at any time, though some implementations may relax this restriction.  The values returned by these procedures are unspecified.

Formal syntax and semantics
***************************

This chapter provides formal descriptions of what has already been described informally in previous chapters of this report.

Formal syntax
=============

This section provides a formal syntax for Scheme written in an extended BNF.  The syntax for the entire language, including features which are not essential, is given here.

All spaces in the grammar are for legibility.  Case is insignificant; for example, #x1A and #X1a are equivalent.  <empty> stands for the empty string.

The following extensions to BNF are used to make the description more concise:  <thing>* means zero or more occurrences of <thing>; and <thing>+ means at least one <thing>.

Lexical structure
-----------------

This section describes how individual tokens (identifiers, numbers, etc.) are formed from sequences of characters.  The following sections describe how expressions and programs are formed from sequences of tokens.

<Intertoken space> may occur on either side of any token, but not within a token.

Tokens which require implicit termination (identifiers, numbers, characters, and dot) may be terminated by any <delimiter>, but not necessarily by anything else.

     <token> --> <identifier> | <boolean> | <number>
          | <character> | <string>
          | ( | ) | #( | ' | ` | , | ,@ | .
     <delimiter> --> <whitespace> | ( | ) | " | ;
     <whitespace> --> <space or newline>
     <comment> --> ;  <all subsequent characters up to a
                      line break>
     <atmosphere> --> <whitespace> | <comment>
     <intertoken space> --> <atmosphere>*

     <identifier> --> <initial> <subsequent>*
          | <peculiar identifier>
     <initial> --> <letter> | <special initial>
     <letter> --> a | b | c | ... | z
     <special initial> --> ! | $ | % | & | * | / | : | < | =
          | > | ? | ~ | _ | ^
     <subsequent> --> <initial> | <digit>
          | <special subsequent>
     <digit> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
     <special subsequent> --> . | + | -
     <peculiar identifier> --> + | -
     <syntactic keyword> --> <expression keyword>
          | else | => | define
          | unquote | unquote-splicing
     <expression keyword> --> quote | lambda | if
          | set! | begin | cond | and | or | case
          | let | let* | letrec | do | delay
          | quasiquote

     <variable> => <any <identifier> which isn't
                     also a <syntactic keyword>>

     <boolean> --> #t | #f
     <character> --> #\ <any character>
          | #\ <character name>
     <character name> --> space | newline

     <string> --> " <string element>* "
     <string element> --> <any character other than " or \>
          | \" | \\ }

     <number> --> <real> | <real> + <ureal> i
              | <real> - <ureal> i | <real> @ <real>
     <real> --> <sign> <ureal>
     <ureal> --> <ureal 2> | <ureal 8> | <ureal 10> | <ureal 16>

The following rules for <ureal R>, <uinteger R>, and <prefix R> should be replicated for R = 2, 8, 10, and 16:

     <ureal R> --> <prefix R> <uinteger R> <suffix>
         | <prefix R> <uinteger R> / <uinteger R> <suffix>
         | <prefix R> . <digit R>+ #* <suffix>
         | <prefix R> <digit R>+ . <digit R>* #* <suffix>
         | <prefix R> <digit R>+ #* . #* <suffix>
     <prefix R> --> <radix R> <exactness> <precision>
              | <radix R> <precision> <exactness>
              | <exactness> <radix R> <precision>
         | <exactness> <precision> <radix R>
         | <precision> <radix R> <exactness>
         | <precision> <exactness> <radix R>
     <uinteger R> --> <digit R>+ #*

     <sign> --> <empty>  | + |  -
     <suffix> --> <empty> | e <sign> <digit>+
     <exactness> --> <empty> | #i | #e
     <precision> --> <empty> | #s | #l
     <radix 2> --> #b
     <radix 8> --> #o
     <radix 10> --> <empty> | #d
     <radix 16> --> #x
     <digit 2> --> 0 | 1
     <digit 8> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
     <digit 10> --> <digit>
     <digit 16> --> <digit> | a | b | c | d | e | f

External representations
------------------------

<Datum> is what the read procedure (section see Input) successfully parses.  Note that any string which parses as an <expression> will also parse as a <datum>.

     <datum> --> <simple datum> | <compound datum>
     <simple datum> --> <boolean> | <number>
          | <character> | <string> |  <symbol>
     <symbol> --> <identifier>
     <compound datum> --> <list> | <vector>
     <list> --> (<datum>*) | (<datum>+ . <datum>)
            | <abbreviation>
     <abbreviation> --> <abbrev prefix> <datum>
     <abbrev prefix> --> ' | ` | , | ,@
     <vector> --> #(<datum>*)

Expressions
-----------

     <expression> --> <variable>
          | <literal>
          | <procedure call>
          | <lambda expression>
          | <conditional>
          | <assignment>
          | <derived expression>

     <literal> --> <quotation> | <self-evaluating>
     <self-evaluating> --> <boolean> | <number>
          | <character> | <string>
     <quotation> --> '<datum> | (quote <datum>)
     <procedure call> --> (<operator> <operand>*)
     <operator> --> <expression>
     <operand> --> <expression>

     <lambda expression> --> (lambda <formals> <body>)
     <formals> --> (<variable>*) | <variable>
          | (<variable>+ . <variable>)
     <body> --> <definition>* <sequence>
     <sequence> --> <command>* <expression>
     <command> --> <expression>

     <conditional> --> (if <test> <consequent> <alternate>)
     <test> --> <expression>
     <consequent> --> <expression>
     <alternate> --> <expression> | <empty>

     <assignment> --> (set! <variable> <expression>)

     <derived expression> -->
            (cond <cond clause>+)
          | (cond <cond clause>* (else <sequence>))
          | (case <expression>
              <case clause>+)
          | (case <expression>
              <case clause>*
              (else <sequence>))
          | (and <test>*)
          | (or <test>*)
          | (let (<binding spec>*) <body>)
          | (let <variable> (<binding spec>*) <body>)
          | (let* (<binding spec>*) <body>)
          | (letrec (<binding spec>*) <body>)
          | (begin <sequence>)
          | (do (<iteration spec>*)
                (<test> <sequence>)
              <command>*)
          | (delay <expression>)
          | <quasiquotation>

     <cond clause> --> (<test> <sequence>)
           | (else <sequence>)
           | (<test>)
           | (<test> => <recipient>)
     <recipient> --> <expression>
     <case clause> --> ((<datum>*) <sequence>)
           | (else <sequence>)

     <binding spec> --> (<variable> <expression>)
     <iteration spec> --> (<variable> <init> <step>)
         | (<variable> <init>)
     <init> --> <expression>
     <step> --> <expression>

Quasiquotations
---------------

The following grammar for quasiquote expressions is not context-free. It is presented as a recipe for generating an infinite number of production rules.  Imagine a copy of the following rules for D = 1, 2,3, ....  D keeps track of the nesting depth.

     <quasiquotation> --> <quasiquotation 1>
     <template 0> --> <expression>
     <quasiquotation D> --> `<template D>
            | (quasiquote <template D>)
     <template D> --> <simple datum>
            | <list template D>
            | <vector template D>
            | <unquotation D>
     <list template D> --> (<template or splice D>*)
            | (<template or splice D>+ . <template D>)
            | '<quasiquotation D>
            | <quasiquotation D+1>
     <vector template D> --> #(<template or splice D>*)
     <unquotation D> --> ,<template D-1>
            | (unquote <template D-1>)
     <template or splice D> --> <template D>
            | <splicing unquotation D>
     <splicing unquotation D> --> ,@<template D-1>
            | (unquote-splicing <template D-1>)

In <quasiquotation>s, a <list template D> can sometimes be confused with an <unquotation D> or <splicing unquotation D>.  The interpretation as an <unquotation> or <splicing unquotation D> takes precedence.

Programs and definitions
------------------------

     <program> --> <command or definition>*
     <command or definition> --> <command> | <definition>
     <definition> --> (define <variable> <expression>)
           | (define (<variable> <def formals>) <body>)
     <def formals> --> <variable>*
           | <variable>* . <variable>

Formal semantics
================

This section provides a formal denotational semantics for the primitive expressions of Scheme and selected built-in procedures.  The concepts and notation used here are described in [STOY77].

     Note: The formal semantics section was written in LaTeX which is incompatible with TeXinfo.  See the Formal semantics section of the original document from which this was derived.

Derived expression types
========================

This section gives rewrite rules for the derived expression types.  By the application of these rules, any expression can be reduced to a semantically equivalent expression in which only the primitive expression types (literal, variable, call, lambda, if, set!) occur.

     (cond (<test> <sequence>)
           <clause2> ...,)
      ==   (if <test>
               (begin <sequence>)
               (cond <clause2> ...,))

     (cond (<test>)
           <clause2> ...,)
      ==   (or <test> (cond <clause2> ...,))

     (cond (<test> => <recipient>)
           <clause2> ...,)
      ==   (let ((test-result <test>)
                 (thunk2 (lambda () <recipient>))
                 (thunk3 (lambda () (cond <clause2> ...,))))
             (if test-result
                 ((thunk2) test-result)
                 (thunk3)))

     (cond (else <sequence>))
      ==   (begin <sequence>)

     (cond)
      ==   <some expression returning an unspecified value>

     (case <key>
       ((d1 ...,) <sequence>)
       ...,)
      ==   (let ((key <key>)
                 (thunk1 (lambda () <sequence>))
                 ...,)
             (cond ((<memv> key '(d1 ...,)) (thunk1))
                    ...,))

     (case <key>
       ((d1 ...,) <sequence>)
       ...,
       (else f1 f2 ...,))
      ==   (let ((key <key>)
                 (thunk1 (lambda () <sequence>))
                 ...,
                 (elsethunk (lambda () ...,)))
             (cond ((<memv> key '(d1 ...,)) (thunk1))
                    ...,
                   (else (elsethunk))))

where <memv> is an expression evaluating to the memv procedure.

     (and)          ==   #t
     (and <test>)   ==   <test>
     (and <test1> <test2> ...,)
      ==   (let ((x <test1>)
                 (thunk (lambda () (and <test2> ...,))))
             (if x (thunk) x))

     (or)           ==   #f
     (or <test>)    ==   <test>
     (or <test1> <test2> ...,)
      ==   (let ((x <test1>)
                 (thunk (lambda () (or <test2> ...,))))
             (if x x (thunk)))

     (let ((<variable1> <init1>) ...,)
       <body>)
      ==   ((lambda (<variable1> ...,) <body>) <init1> ...,)

     (let* () <body>)
      ==   ((lambda () <body>))
     (let* ((<variable1> <init1>)
            (<variable2> <init2>)
            ...,)
       <body>)
      ==   (let ((<variable1> <init1>))
             (let* ((<variable2> <init2>)
                    ...,)
               <body>))

     (letrec ((<variable1> <init1>)
              ...,)
       <body>)
      ==   (let ((<variable1> <undefined>)
                 ...,)
              (let ((<temp1> <init1>)
                    ...,)
                (set! <variable1> <temp1>)
                ...,)
              <body>)

where <temp1>, <temp2>, ..., are variables, distinct from <variable1>, ...,, which do not free occur in the original <init> expressions, and <undefined> is an expression which returns something which when stored in a location makes it an error to try to obtain the value stored in the location.  (No such expression is defined, but one is assumed to exist for the purposes of this rewrite rule.)  The second let expression in the expansion is not strictly necessary, but it serves to preserve the property that the <init> expressions are evaluated in an arbitrary order.

     (begin <sequence>)
      ==   ((lambda () <sequence>))

The following alternative expansion for begin does not make use of the ability to write more than one expression in the body of a lambda expression.  In any case, note that these rules apply only if <sequence> contains no definitions.

     (begin <expression>) ==   <expression>
     (begin <command> <sequence>)
      ==   ((lambda (ignore thunk) (thunk))
            <command>
            (lambda () (begin <sequence>)))

     (do ((<variable1> <init1> <step1>)
          ...,)
         (<test> <sequence>)
       <command1> ...,)
      ==   (letrec ((<loop>
                     (lambda (<variable1> ...,)
                       (if <test>
                           (begin <sequence>)
                           (begin <command1>
                                  ...,
                                  (<loop> <step1> ...,))))))
             (<loop> <init1> ...,))

where <loop> is any variable which is distinct from <variable1>, ...,, and which does not occur free in the do expression.

     (let <variable_0> ((<variable1> <init1>) ...,)
       <body>)
      ==   ((letrec ((<variable_0> (lambda (<variable1> ...,)
                                  <body>)))
               <variable_0>)
            <init1> ...,)

     (delay <expression>)
      ==   (<make-promise> (lambda () <expression>))

where <make-promise> is an expression evaluating to some procedure which behaves appropriately with respect to the force procedure; see section Control features.

Notes
*****

Language changes
================

This section enumerates the changes that have been made to Scheme since the ``Revised revised report'' [RRRS] was published.

  * The character ^ (circumflex) is now an extended alphabetic character

  * The objects returned by literal expressions are permitted to be immutable

  * The list to which a rest-argument becomes bound must be newly allocated

  * Do variables are updated by rebinding rather than by assignment

  * New expression type: delay

  * Quasiquote (backquote) has been improved in several ways: vectors are allowed; nesting is allowed; and an external syntax for quasiquote expressions (analogous to that for quote) has been defined

  * The semantics of definitions of the form (define (<variable> <formals>) <body>) no longer involves an implicit rec or letrec

  * The ``curried'' definition syntax has been removed

  * The boolean constants are now written #t and #f instead of #!true and #!false

  * The syntax #!null (for the empty list) has been removed

  * New procedures: boolean?, procedure?, and force

  * The value of eq? on numbers and characters is now unspecified

  * Eq? and eqv? now explicitly permit operationally equivalent procedures to be identified

  * Eqv? distinguishes exact numbers from inexact ones, even if they are equal according to =

  * List, string, and vector indexes must be exact integers

  * Atan now admits either one or two arguments

  * Expression types removed: named-lambda, rec, sequence

  * Procedures removed: append!, string-null?, substring-fill!, substring-move-left!, substring-move-right!, object-hash, object-unhash, 1+, -1+

  * Redundant procedure names removed: <?, <=?, =?, >?, and >=?

Keywords as variable names
==========================

Some implementations allow arbitrary syntactic keywords to be used as variable names, instead of reserving them, as this report would have it. But this creates ambiguities in the interpretation of expressions: for example, in the following, it's not clear whether the expression (if 1 2 3) should be treated as a procedure call or as a conditional.

     (define if list)
     (if 1 2 3)                             ==>  2 or (1 2 3)

These ambiguities are usually resolved in some consistent way within any given implementation, but no particular treatment stands out as being clearly superior to any other, so these situations were excluded for the purposes of this report.

Macros
======

Scheme does not have any standard facility for defining new kinds of expressions.

The ability to alter the syntax of the language creates numerous problems.  All current implementations of Scheme have macro facilities that solve those problems to one degree or another, but the solutions are quite different and it isn't clear at this time which solution is best, or indeed whether any of the solutions are truly adequate.  Rather than standardize, we are encouraging implementations to continue to experiment with different solutions.

The main problems with traditional macros are: They must be defined to the system before any code using them is loaded; this is a common source of obscure bugs.  They are usually global; macros can be made to follow lexical scope rules , but many people find the resulting scope rules confusing.  Unless they are written very carefully, macros are vulnerable to inadvertant capture of free variables; to get around this, for example, macros may have to generate code in which procedure values appear as quoted constants.  There is a similar problem with syntactic keywords if the keywords of special forms are not reserved.  If keywords are reserved, then either macros introduce new reserved words, invalidating old code, or else special forms defined by the programmer do not have the same status as special forms defined by the system.

Example
*******

Integrate-system integrates the system

y_k^^ = f_k(y_1, y_2, ..., y_n),    k = 1, ..., n

of differential equations with the method of Runge-Kutta.

The parameter system-derivative is a function that takes a system state (a vector of values for the state variables y_1, ..., y_n) and produces a system derivative (the values y_1^^, ...,y_n^^).  The parameter initial-state provides an initial system state, and h is an initial guess for the length of the integration step.

The value returned by integrate-system is an infinite stream of system states.

     (define integrate-system
       (lambda (system-derivative initial-state h)
         (let ((next (runge-kutta-4 system-derivative h)))
           (letrec ((states
                     (cons initial-state
                           (delay (map-streams next
                                               states)))))
             states))))

Runge-Kutta-4 takes a function, f, that produces a system derivative from a system state.  Runge-Kutta-4 produces a function that takes a system state and produces a new system state.

     (define runge-kutta-4
       (lambda (f h)
         (let ((*h (scale-vector h))
               (*2 (scale-vector 2))
               (*1/2 (scale-vector (/ 1 2)))
               (*1/6 (scale-vector (/ 1 6))))
           (lambda (y)
             ;; y is a system state
             (let* ((k0 (*h (f y)))
                    (k1 (*h (f (add-vectors y (*1/2 k0)))))
                    (k2 (*h (f (add-vectors y (*1/2 k1)))))
                    (k3 (*h (f (add-vectors y k2)))))
               (add-vectors y
                 (*1/6 (add-vectors k0
                                    (*2 k1)
                                    (*2 k2)
                                    k3))))))))

     (define elementwise
       (lambda (f)
         (lambda vectors
           (generate-vector
             (vector-length (car vectors))
             (lambda (i)
               (apply f
                      (map (lambda (v) (vector-ref  v i))
                           vectors)))))))

     (define generate-vector
       (lambda (size proc)
         (let ((ans (make-vector size)))
           (letrec ((loop
                     (lambda (i)
                       (cond ((= i size) ans)
                             (else
                              (vector-set! ans i (proc i))
                              (loop (+ i 1)))))))
             (loop 0)))))

     (define add-vectors (elementwise +))

     (define scale-vector
       (lambda (s)
         (elementwise (lambda (x) (* x s)))))

Map-streams is analogous to map: it applies its first argument (a procedure) to all the elements of its second argument (a stream).

     (define map-streams
       (lambda (f s)
         (cons (f (head s))
               (delay (map-streams f (tail s))))))

Infinite streams are implemented as pairs whose car holds the first element of the stream and whose cdr holds a promise to deliver the rest of the stream.

     (define head car)
     (define tail
       (lambda (stream) (force (cdr stream))))

The following illustrates the use of integrate-system in integrating the system

C dv_C / dt = -i_L - v_C / R

L di_L / dt = v_C

which models a damped oscillator.

     (define damped-oscillator
       (lambda (R L C)
         (lambda (state)
           (let ((Vc (vector-ref state 0))
                 (Il (vector-ref state 1)))
             (vector (- 0 (+ (/ Vc (* R C)) (/ Il C)))
                     (/ Vc L))))))

     (define the-states
       (integrate-system
          (damped-oscillator 10000 1000 .001)
          '#(1 0)
          .01))

Bibliography
************

  * [SICP] Harold Abelson and Gerald Jay Sussman with Julie Sussman. Structure and Interpretation of Computer Programs. MIT Press, Cambridge, 1985.

  * [Bartley86] David H. Bartley and John C. Jensen. The implementation of PC Scheme. In Proceedings of the 1986 ACM Conference on Lisp and Functional Programming, pages 86--93.

  * [Scheme81] John Batali, Edmund Goodhue, Chris Hanson, Howie Shrobe, Richard M. Stallman, and Gerald Jay Sussman. The Scheme-81 architecture---system and chip. In Proceedings, Conference on Advanced Research in VLSI, pages 69--77. Paul Penfield, Jr., editor. Artech House, 610 Washington Street, Dedham MA, 1982.

  * [RRRS] William Clinger, editor. The revised revised report on Scheme, or an uncommon Lisp. MIT Artificial Intelligence Memo 848, August 1985. Also published as Computer Science Department Technical Report 174, Indiana University, June 1985.

  * [Clinger84] William Clinger. The Scheme 311 compiler: An exercise in denotational semantics. In Conference Record of the 1984 ACM Symposium on Lisp and Functional Programming, pages 356--364.

  * [Dybvig86] R. Kent Dybvig, Daniel P. Friedman, and Christopher T. Haynes. Expansion-passing style: Beyond conventional macros. In Proceedings of the 1986 ACM Conference on Lisp and Functional Programming, pages 143--150.

  * [Eisenberg85] Michael A. Eisenberg. Bochser: an integrated Scheme programming system. MIT Laboratory for Computer Science Technical Report 349, October 1985.

  * [Feeley86] Marc Feeley. Deux approches `a l'implantation du language Scheme. M.Sc. thesis, D'epartement d'Informatique et de Recherche Op'erationelle, University of Montreal, May 1986.

  * [Felleisen86] Matthias Felleisen, Daniel P. Friedman, Eugene Kohlbecker, and Bruce Duba. Reasoning with continuations. In Proceedings of the Symposium on Logic in Computer Science, pages 131--141. IEEE Computer Society Press, Washigton DC, 1986.

  * [Scheme311] Carol Fessenden, William Clinger, Daniel P. Friedman, and Christopher Haynes. Scheme 311 version 4 reference manual. Indiana University Computer Science Technical Report 137, February 1983. Superceded by [Scheme84].

  * [Lisper] Daniel P. Friedman and Matthias Felleisen. The Little LISPer. Science Research Associates, second edition 1986.

  * [Friedman84] Daniel P. Friedman, Christopher T. Haynes, and Eugene Kohlbecker. Programming with continuations. In Program Transformation and Programming Environments, pages 263--274. P. Pepper, editor. Springer-Verlag, 1984.

  * [Scheme84] D. Friedman, C. Haynes, E. Kohlbecker, and M. Wand. Scheme 84 interim reference manual. Indiana University Computer Science Technical Report 153, January 1985.

  * [Friedman85] Daniel P. Friedman and Christopher T. Haynes. Constraining control. In Proceedings of the Twelfth Annual Symposium on Principles of Programming Languages, pages 245--254. ACM, January 1985.

  * [Haynes84] Christopher T. Haynes, Daniel P. Friedman, and Mitchell Wand. Continuations and coroutines. In Conference Record of the 1984 ACM Symposium on Lisp and Functional Programming, pages 293--298.

  * [Haynes86] Christopher T. Haynes. Logic continuations. In Proceedings of the Third International Conference on Logic Programming, pages 671--685. Springer-Verlag, July 1986.

  * [Engines] Christopher T. Haynes and Daniel P. Friedman. Engines build process abstracions. In Conference Record of the 1984 ACM Symposium on Lisp and Functional Programming, pages 18--24.

  * [Henderson82] Peter Henderson.  Functional Geometry. In Conference Record of the 1982 ACM Symposium on Lisp and Functional Programming, pages 179--187.

  * [Kohlbecker86] Eugene Edmund Kohlbecker Jr. Syntactic Extensions in the Programming Language Lisp. PhD thesis, Indiana University, August 1986.

  * [Kranz86] David Kranz, Richard Kelsey, Jonathan Rees, Paul Hudak, James Philbin, and Norman Adams. Orbit: An optimizing compiler for Scheme. In Proceedings of the SIGPLAN '86 Symposium on Compiler Construction, pages 219--233. ACM, June 1986. Proceedings published as SIGPLAN Notices 21(7), July 1986.

  * [Landin65] Peter Landin. A correspondence between Algol 60 and Church's lambda notation: Part I. Communications of the ACM 8(2):89--101, February 1965.

  * [McDermott80] Drew McDermott. An efficient environment allocation scheme in an interpreter for a lexically-scoped lisp. In Conference Record of the 1980 Lisp Conference, pages 154--162. The Lisp Conference, P.O. Box 487, Redwood Estates CA, 1980. Proceedings reprinted by ACM.

  * [MITScheme] MIT Department of Electrical Engineering and Computer Science. Scheme manual, seventh edition. September 1984.

  * [Muchnick80] Steven S. Muchnick and Uwe F. Pleban. A semantic comparison of Lisp and Scheme. In Conference Record of the 1980 Lisp Conference, pages 56--64. The Lisp Conference, 1980.

  * [Naur63] Peter Naur et al. Revised report on the algorithmic language Algol 60. Communications of the ACM 6(1):1--17, January 1963.

  * [Penfield81] Paul Penfield, Jr. Principal values and branch cuts in complex APL. In APL '81 Conference Proceedings, pages 248--256. ACM SIGAPL, San Francisco, September 1981. Proceedings published as APL Quote Quad 12(1), ACM, September 1981.

  * [Pitman85] Kent M. Pitman. Exceptional situations in Lisp. MIT Artificial Intelligence Laboratory Working Paper 268, February 1985.

  * [Pitman83] Kent M. Pitman. The revised MacLisp manual (saturday evening edition). MIT Artificial Intelligence Laboratory Technical Report 295, May 1983.

  * [Pitman80] Kent M. Pitman. Special forms in Lisp. In Conference Record of the 1980 Lisp Conference, pages 179--187. The Lisp Conference, 1980.

  * [Rees82] Jonathan A. Rees and Norman I. Adams IV. T: A dialect of Lisp or, lambda: The ultimate software tool. In Conference Record of the 1982 ACM Symposium on Lisp and Functional Programming, pages 114--122.

  * [Rees84] Jonathan A. Rees, Norman I. Adams IV, and James R. Meehan. The T manual, fourth edition. Yale University Computer Science Department, January 1984.

  * [Reynolds72] John Reynolds. Definitional interpreters for higher order programming languages. In ACM Conference Proceedings, pages 717--740. ACM, 1972.

  * [Rozas84] Guillermo J. Rozas. Liar, an Algol-like compiler for Scheme. S. B. thesis, MIT Department of Electrical Engineering and Computer Science, January 1984.

  * [Smith84] Brian C. Smith. Reflection and semantics in a procedural language. MIT Laboratory for Computer Science Technical Report 272, January 1982.

  * [Srivastava85] Amitabh Srivastava, Don Oxley, and Aditya Srivastava. An(other) integration of logic and functional programming. In Proceedings of the Symposium on Logic Programming, pages 254--260. IEEE, 1985.

  * [Stallman80] Richard M. Stallman. Phantom stacks---if you look too hard, they aren't there. MIT Artificial Intelligence Memo 556, July 1980.

  * [Imperative] Guy Lewis Steele Jr. and Gerald Jay Sussman. Lambda, the ultimate imperative. MIT Artificial Intelligence Memo 353, March 1976.

  * [Declarative] Guy Lewis Steele Jr. Lambda, the ultimate declarative. MIT Artificial Intelligence Memo 379, November 1976.

  * [Debunking] Guy Lewis Steele Jr. Debunking the ``expensive procedure call'' myth, or procedure call implementations considered harmful, or lambda, the ultimate GOTO. In ACM Conference Proceedings, pages 153--162. ACM, 1977.

  * [Macaroni] Guy Lewis Steele Jr. Macaroni is better than spaghetti. In Proceedings of the Symposium on Artificial Intelligence and Programming Languages, pages 60--66. These proceedings were published as a special joint issue of SIGPLAN Notices 12(8) and SIGART Newsletter 64, August 1977.

  * [Rabbit] Guy Lewis Steele Jr. Rabbit: a compiler for Scheme. MIT Artificial Intelligence Laboratory Technical Report 474, May 1978.

  * [CLoverview] Guy Lewis Steele Jr. An overview of Common Lisp. In Conference Record of the 1982 ACM Symposium on Lisp and Functional Programming, pages 98--107.

  * [CLtL] Guy Lewis Steele, Jr. Common Lisp: The Language. Digital Press, Burlington MA, 1984.

  * [Scheme78] Guy Lewis Steele, Jr. and Gerald Jay Sussman. The revised report on Scheme, a dialect of Lisp. MIT Artificial Intelligence Memo 452, January 1978.

  * [TAOTI] Guy Lewis Steele, Jr. and Gerald Jay Sussman. The art of the interpreter, or the modularity complex (parts zero, one, and two). MIT Artificial Intelligence Memo 453, May 1978.

  * [DOALBP] Guy Lewis Steele, Jr. and Gerald Jay Sussman. Design of a Lisp-based processor. Communications of the ACM 23(11):628--645, November 1980.

  * [Dream] Guy Lewis Steele, Jr. and Gerald Jay Sussman. The dream of a lifetime: a lazy variable extent mechanism. In Conference Record of the 1980 Lisp Conference, pages 163--172. The Lisp Conference, 1980.

  * [Scheme75] Gerald Jay Sussman and Guy Lewis Steele, Jr. Scheme: an interpreter for extended lambda calculus. MIT Artificial Intelligence Memo 349, December 1975.

  * [Scheme79] Gerald Jay Sussman, Jack Holloway, Guy Lewis Steele, Jr., and Alan Bell. Scheme-79---Lisp on a chip. IEEE Computer 14(7):10--21, July 1981.

  * [Stoy77] Joseph E. Stoy. Denotational Semantics: The Scott-Strachey Approach to Programming Language Theory. MIT Press, Cambridge, 1977.

  * [TI85] Texas Instruments, Inc. TI Scheme Language Reference Manual. Preliminary version 1.0, November 1985.

  * [Wand78] Mitchell Wand. Continuation-based program transformation strategies. Journal of the ACM 27(1):174--180, 1978.

  * [Wand80] Mitchell Wand. Continuation-based multiprocessing. In Conference Record of the 1980 Lisp Conference, pages 19--28. The Lisp Conference, 1980.

Alphabetic index of definitions of concepts, keywords, and procedures
*********************************************************************

The principal entry for each term, procedure, or keyword is listed first, separated from the other entries by a semicolon.