\input texinfo @c -*-texinfo-*- @c %**start of header @setfilename r3rs.info @settitle Revised(3) Scheme @c \pagestyle{headings} @c \showboxdepth=0 @c \def\theevenhead{Revised$^3$ Scheme} @c \hfil {\bf *** DRAFT --- \today{} ***} @paragraphindent 0 @c %**end of header @c syncodeindex fn cp @ifinfo @dircategory The Algorithmic Language Scheme @direntry * R3RS: (r3rs). The Revised(3) Report on Scheme. @end direntry @end ifinfo @c @include{first} @titlepage @c HTML first page @title Scheme @subtitle Revised(3) Report on the Algorithmic Language Scheme @c First page @c \thispagestyle{empty} @c \todo{"another" report?} @author J@sc{ONATHAN} R@sc{EES AND} W@sc{ILLIAM} C@sc{LINGER} (@i{Editors}) @author H. A@sc{BELSON} @author R. K. D@sc{YBVIG} @author C. T. H@sc{AYNES} @author G. J. R@sc{OZAS} @author N. I. A@sc{DAMS IV} @author D. P. F@sc{RIEDMAN} @author E. K@sc{OHLBECKER} @author G. J. S@sc{USSMAN} @author D. H. B@sc{ARTLEY} @author R. H@sc{ALSTEAD} @author D. O@sc{XLEY} @author M. W@sc{AND} @author G. B@sc{ROOKS} @author C. H@sc{ANSON} @author K. M. P@sc{ITMAN} @author @i{Dedicated to the Memory of ALGOL 60} @c {\it Dedicated to the Memory of D{\sc\it YNAMIC} B{\sc\it INDING}} @unnumbered 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 @ref{Expressions} and @ref{Program structure} describe the syntax and semantics of expressions, programs, and definitions. Chapter @ref{Standard procedures} describes Scheme's built-in procedures, which include all of the language's data manipulation and input/output primitives. Chapter @ref{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. @ignore todo expand the summary so that it fills up the column. @end ignore @c \vfill @c \begin{center} @c {\large \bf @c *** DRAFT*** \\ @c \today @c }\end{center} @page @end titlepage @c INFO first page @ifinfo @c First page @c \thispagestyle{empty} @c \todo{"another" report?} @node top, Introduction, (dir), (dir) @top Revised(3) Report on the Algorithmic Language Scheme @sp 1 @center @c begin-tabular @quotation @multitable @columnfractions 0.25 0.25 0.25 0.25 @item @center J@sc{ONATHAN} R@sc{EES AND} W@sc{ILLIAM} C@sc{LINGER} (@i{Editors}) @item H. A@sc{BELSON} @tab R. K. D@sc{YBVIG} @tab C. T. H@sc{AYNES} @tab G. J. R@sc{OZAS} @item N. I. A@sc{DAMS IV} @tab D. P. F@sc{RIEDMAN} @tab E. K@sc{OHLBECKER} @tab G. J. S@sc{USSMAN} @item D. H. B@sc{ARTLEY} @tab R. H@sc{ALSTEAD} @tab D. O@sc{XLEY} @tab M. W@sc{AND} @item G. B@sc{ROOKS} @tab C. H@sc{ANSON} @tab K. M. P@sc{ITMAN} @item @end multitable @end quotation @sp 2 @i{Dedicated to the Memory of ALGOL 60} @c {\it Dedicated to the Memory of D{\sc\it YNAMIC} B{\sc\it INDING}} @sp 3 @majorheading 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 @ref{Expressions} and @ref{Program structure} describe the syntax and semantics of expressions, programs, and definitions. Chapter @ref{Standard procedures} describes Scheme's built-in procedures, which include all of the language's data manipulation and input/output primitives. Chapter @ref{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. @ignore todo expand the summary so that it fills up the column. @end ignore @c \vfill @c \begin{center} @c {\large \bf @c *** DRAFT*** \\ @c \today @c }\end{center} @unnumbered Contents @menu * Introduction:: * Overview of Scheme:: * Lexical conventions:: * Basic concepts:: * Expressions:: * Program structure:: * Standard procedures:: * Formal syntax and semantics:: * Notes:: * Example:: * Bibliography:: * Index:: @end menu @page @end ifinfo @c @include{intro} @node Introduction, Overview of Scheme, top, top @unnumbered Introduction @menu * Background:: * Acknowledgements:: @end menu 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. @ignore todo Ramsdell: I would like to make a few comments on presentation. The most important comment is about section organization. Newspaper writers spend most of their time writing the first three paragraphs of any article. This part of the article is often the only part read by readers, and is important in enticing readers to continue. In the same way, The first page is most likely to be the only page read by many SIGPLAN readers. If I had my choice of what I would ask them to read, it would be the material in section 1.1, the Semantics section that notes that scheme is lexically scoped, tail recursive, weakly typed, ... etc. I would expand on the discussion on continutations, as they represent one important difference between Scheme and other languages. The introduction, with its history of scheme, its history of scheme reports and meetings, and acknowledgements giving names of people that the reader will not likely know, is not that one page I would like all to read. I suggest moving the history to the back of the report, and use the first couple of pages to convince the reader that the language documented in this report is worth studying. @end ignore @node Background, Acknowledgements, Introduction, Introduction @unnumberedsec Background The first description of Scheme was written in 1975 [Scheme75]. A revised report [Scheme78] @ignore todo italicize or not? @end ignore 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. @c \vest The number 3 in the title is part of the title, not a reference to @c a footnote. The word ``revised'' is raised to the third power because @c the report is a revision of a report that was already twice revised. @ignore todo Write an editors' note? @end ignore @sp 3 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. @node Acknowledgements, , Background, Introduction @unnumberedsec 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 @emph{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. @ignore todo Steele: [c] There should be a very clear message to the reader that Scheme certainly does owe debts to other sources, and one of them is Common Lisp. While Scheme certainly has been the pioneer in the treatment of closures and functional programming in a Lisp framework, I think it is fair to say that Common lisp pioneered a rational (forgive the pun) treatment of numeric data types in a Lisp framework, and my impression is that Scheme learned something in this area from the Common Lisp experience. @end ignore @sp 2 @c \clearchapterstar{Description of the language} %\unskip\vskip -2ex @c \chapter*{Description of the language}\unskip\vskip -2ex @c @include{struct} @c 1. Structure of the language @node Overview of Scheme, Lexical conventions, Introduction, top @chapter Overview of Scheme @menu * Semantics:: * Syntax:: * Notation and terminology:: @end menu @node Semantics, Syntax, Overview of Scheme, Overview of Scheme @section Semantics This section gives an overview of Scheme's semantics. A detailed informal semantics is the subject of chapters @ref{Basic concepts} through @ref{Standard procedures}. For reference purposes, section @ref{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. @ignore todo Rozas: Scheme had them first. @end ignore 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 @ref{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. @ignore todo Lisp's call by value should be explained more accurately. What's funny is that all values are references. @end ignore @node Syntax, Notation and terminology, Semantics, Overview of Scheme @section 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 @code{read} primitive @vindex @w{read} parses its input; that is, it performs syntactic as well as lexical decomposition of what it reads. @node Notation and terminology, , Syntax, Overview of Scheme @section Notation and terminology @menu * Essential and non-essential features:: * Error situations and unspecified behavior:: * Entry format:: * Evaluation examples:: @end menu @node Essential and non-essential features, Error situations and unspecified behavior, Notation and terminology, Notation and terminology @subsection Essential and non-essential features It is required that every implementation of Scheme support features that are marked as being @dfn{essential}. Features not @cindex @w{essential} 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. @node Error situations and unspecified behavior, Entry format, Essential and non-essential features, Notation and terminology @subsection Error situations and unspecified behavior @cindex @w{error} 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. @ignore todo Talk about unspecified behavior vs. unspecified values. @end ignore @ignore todo Look at KMP's situations paper. @end ignore @node Entry format, Evaluation examples, Error situations and unspecified behavior, Notation and terminology @subsection Entry format Chapters @ref{Expressions} and @ref{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 @noindent @deffn {essential @var{category}} @var{template} @end deffn if the feature is an essential feature, or simply @noindent @deffn {@var{category}} @var{template} @end deffn if the feature is not an essential feature. If @var{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, @r{}, @r{}. Syntactic variables should be understood to denote segments of program text; for example, @r{} stands for any string of characters which is a syntactically valid expression. The notation @format @r{} @dots{} @end format indicates zero or more occurrences of a @r{}, and @format @r{} @r{} @dots{} @end format indicates one or more occurrences of a @r{}. If @var{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 @var{italicized}. Thus the header line @noindent @deffn {essential procedure} (vector-ref @var{vector} @var{k}) @end deffn indicates that the essential built-in procedure @t{vector-ref} takes two arguments, a vector @var{vector} and an exact non-negative integer @var{k} (see below). The header lines @noindent @deffn {essential procedure} (append @var{list1} @var{list2}) @deffnx {procedure} (append @var{list} @dots{},) @end deffn indicate that in all implementations, the @t{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 @t{vector-ref} given above dictates that first argument to @t{vector-ref} must be a vector. The following naming conventions also imply type restrictions: @c \newcommand{\foo}[1]{\vr{#1}, \vri{#1}, $\ldots$ \vrj{#1}, $\ldots$} @center @c begin-tabular @quotation @table @asis @item @var{obj} any object @item @var{z}, @var{z1}, @dots{} @var{zj}, @dots{} complex, real, rational, integer @item @var{x}, @var{x1}, @dots{} @var{xj}, @dots{} real, rational, integer @item @var{y}, @var{y1}, @dots{} @var{yj}, @dots{} real, rational, integer @item @var{q}, @var{q1}, @dots{} @var{qj}, @dots{} rational, integer @item @var{n}, @var{n1}, @dots{} @var{nj}, @dots{} integer @item @var{k}, @var{k1}, @dots{} @var{kj}, @dots{} exact non-negative integer @item @end table @end quotation @ignore todo Provide an example entry?? @end ignore @node Evaluation examples, , Entry format, Notation and terminology @subsection Evaluation examples The symbol ``@result{}'' used in program examples should be read ``evaluates to.'' For example, @example (* 5 8) ==> 40 @end example means that the expression @t{(* 5 8)} evaluates to the object @t{40}. Or, more precisely: the expression given by the sequence of characters ``@t{(* 5 8)}'' evaluates, in the initial environment, to an object that may be represented externally by the sequence of characters ``@t{40}''. See section @ref{External representations} for a discussion of external representations of objects. @ignore todo Naming conventions @end ignore @ignore todo Terms that need defining: thunk, command, mutation procedure, (what else?). The @t{?}, @t{!}, and @t{->} conventions should be explained somewhere. @end ignore @ignore todo Just say somewhere that the value of procedures whose names end in @t{!} is unspecified. @end ignore @c @include{lex} @c Lexical structure @c %\vfill\eject @node Lexical conventions, Basic concepts, Overview of Scheme, top @chapter Lexical conventions @menu * Identifiers:: * Whitespace and comments:: * Other notations:: @end menu 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 @ref{Formal syntax}. Upper and lower case forms of a letter are never distinguished except within character and string constants. For example, @code{Foo} is @vindex @w{Foo} the same identifier as @code{FOO}, and @t{#x1AB} is the same number as @vindex @w{FOO} @t{#X1ab}. @node Identifiers, Whitespace and comments, Lexical conventions, Lexical conventions @section Identifiers Most identifiers allowed by other programming @cindex @w{identifier} 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, @code{+} and @code{-} (which can begin @vindex @w{-} @vindex @w{+} numbers) are identifiers. Here are some examples of identifiers: @example lambda q list->vector soup + V17a <=? a34kTMNs the-word-recursion-has-many-meanings @end example @c _ = 5F & = 26 ~ = 7E ^ = 5E Extended alphabetic characters may be used in identifiers exactly as if they were letters. The following are extended alphabetic characters: @example * / < = > ! ? : $ % _ & ~ ^ @end example See section @ref{Lexical structure} for a formal syntax of identifiers. Identifiers have several uses within Scheme programs: @itemize @bullet @item Certain identifiers are reserved for use as syntactic keywords (see below). @c (This does not preclude their use as identifiers @c as well, although in certain situations ambiguities can result if this is done.) @item Any identifier that is not a syntactic keyword may be used as a variable (see section @pxref{Variables and regions}). @item When an identifier appears as a literal or within a literal (see section @pxref{Literal expressions}), it is being used to denote a @emph{symbol} (see section @pxref{Symbols}). @end itemize @cindex @w{variable} @cindex @w{keyword} @cindex @w{syntactic keyword} The following identifiers are syntactic keywords, and should not be used as variables: @example => do or and else quasiquote begin if quote case lambda set! cond let unquote define let* unquote-splicing delay letrec @end example 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. @ignore todo The Miller vs. Dybvig debate over reserving new keywords. What to do? @end ignore The characters @t{?} and @t{!} 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 @t{?}, and most data mutation procedures are named by identifiers that end in @t{!}. @vindex ? @vindex ! @node Whitespace and comments, Other notations, Identifiers, Lexical conventions @section Whitespace and comments @dfn{Whitespace} characters are spaces and newlines. @cindex @w{Whitespace} (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 (@t{;}) indicates the start of a comment. The comment continues to the @cindex @w{;} @cindex @w{comment} 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. @example ;;; 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)))))) @end example @node Other notations, , Whitespace and comments, Lexical conventions @section Other notations @ignore todo Rewrite? @end ignore For a description of the notations used for numbers, see section @ref{Numbers}. @table @t @item @t{.@: + -} 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 @pxref{Pairs and lists}), and to indicate a rest-parameter in a formal parameter list (section @pxref{lambda expressions}). @item @t{( )} Parentheses are used for grouping and to notate lists (section @pxref{Pairs and lists}). @item @t{'} The single quote character is used to indicate literal data (section @pxref{Literal expressions}). @item @t{`} The backquote character is used to indicate almost-constant data (section @pxref{Quasiquotation}). @item @t{, ,@@} The character comma and the sequence comma at-sign are used in conjunction with backquote (section @pxref{Quasiquotation}). @item @t{"} The double quote character is used to delimit strings (section @pxref{Strings}). @item \ Backslash is used in the syntax for character constants and as an escape character within string constants (section @pxref{Strings}). @item @w{@t{[ ] @{ @}}} Left and right square brackets and curly braces are reserved for possible future extensions to the language. @item # Sharp sign is used for a variety of purposes depending on the character that immediately follows it: @item @t{#t} @t{#f} These are the boolean constants (section @pxref{Booleans}). @item #\ This introduces a character constant (section @pxref{Characters}). @item #@t{(} This introduces a vector constant (section @pxref{Vectors}). Vector constants are terminated by @t{)} . @item @t{#e #i #l #s #b #o #d #x} These are used in the notation for numbers (section @pxref{Number syntax}). @end table @c @include{basic} @c \vfill\eject @node Basic concepts, Expressions, Lexical conventions, top @chapter Basic concepts @menu * Variables and regions:: * True and false:: * External representations:: @end menu @node Variables and regions, True and false, Basic concepts, Basic concepts @section Variables and regions Any identifier that is not a syntactic keyword (see @cindex @w{keyword} section @pxref{Identifiers}) may be used as a variable. A variable may name a @cindex @w{variable} @cindex @w{identifier} @cindex @w{syntactic keyword} location where a value can be stored. A variable that does so is said to be @emph{bound} to the location. The set of all such bindings in effect at some point in a program is @cindex @w{binding} known as the @emph{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. @ignore todo Define ``assigned'' and ``unassigned'' perhaps? @end ignore @ignore todo In programs without side effects, one can safely pretend that the variables are bound directly to the arguments. Or: In programs without @code{set!}, one can safely pretend that the @vindex @w{set!} variable is bound directly to the value. @end ignore Certain expression types are used to create new locations and to bind variables to those locations. The most fundamental of these @emph{binding constructs} is the @cindex @w{binding construct} lambda expression, because all other binding constructs @cindex @w{lambda expression} can be explained in terms of lambda expressions. The other binding constructs are @code{let}, @code{let*}, @code{letrec}, and @code{do} @vindex @w{do} @vindex @w{letrec} @vindex @w{let*} @vindex @w{let} expressions (see sections @pxref{lambda expressions}, @pxref{Binding constructs}, and @pxref{Iteration}). @c Note: internal definitions not mentioned here. 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 @dfn{region} of the program text within which @cindex @w{region} 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 @pxref{Standard procedures}); if there is no binding for the identifier, it is said to be @dfn{unbound}. @cindex @w{top level environment} @cindex @w{bound} @cindex @w{unbound} @ignore todo Mention that some implementations have multiple top level environments? @end ignore @ignore todo Pitman sez: needs elaboration in case of @t{(let ...)} @end ignore @ignore todo Pitman asks: say something about vars created after scheme starts? @t{(define x 3) (define (f) x) (define (g) y) (define y 4)} Clinger replies: The language was explicitly designed to permit a view in which no variables are created after Scheme starts. In files, you can scan out the definitions beforehand. I think we're agreed on the principle that interactive use should approximate that behavior as closely as possible, though we don't yet agree on which programming environment provides the best approximation. @end ignore @node True and false, External representations, Variables and regions, Basic concepts @section True and false Any Scheme value can be used as a boolean value for the purpose of a conditional test. As explained in section @ref{Booleans}, all values count as true in such a test except for @t{#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. @cindex @w{false} @cindex @w{true} @ignore todo Bartley: tighten this up. @end ignore @node External representations, , True and false, Basic concepts @section External representations An important concept in Scheme (and Lisp) is that of the @emph{external representation} of an object as a sequence of characters. For example, an external representation of the integer 28 is the sequence of characters ``@t{28}'', and an external representation of a list consisting of the integers 8 and 13 is the sequence of characters ``@t{(8 13)}''. The external representation of an object is not necessarily unique. The integer 28 also has representations ``@t{28.000}'' and ``@t{#x1c}'', and the list in the previous paragraph also has the representations ``@t{( 08 13 )}'' and ``@t{(8 .@: (13 .@: ()))}'' (see section @pxref{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 @code{quote}, section @pxref{Literal expressions}). @vindex @w{quote} External representations can also be used for input and output. The procedure @code{read} (section @pxref{Input}) parses external @vindex @w{read} representations, and the procedure @code{write} (section @pxref{Output}) @vindex @w{write} generates them. Together, they provide an elegant and powerful input/output facility. Note that the sequence of characters ``@t{(+ 2 6)}'' is @emph{not} an external representation of the integer 8, even though it @emph{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 @t{+} 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 @ref{Standard procedures}. @c @include{expr} @c \vfill\eject @node Expressions, Program structure, Basic concepts, top @chapter Expressions @menu * Primitive expression types:: * Derived expression types:: @end menu @c \newcommand{\syntax}{{\em Syntax: }} @c \newcommand{\semantics}{{\em Semantics: }} 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 @emph{primitive} or @emph{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 @ref{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. @node Primitive expression types, Derived expression types, Expressions, Expressions @section Primitive expression types @menu * Variable references:: * Literal expressions:: * Procedure calls:: * lambda expressions:: * Conditionals:: * Assignments:: @end menu @node Variable references, Literal expressions, Primitive expression types, Primitive expression types @subsection Variable references @deffn {essential syntax} @r{} An expression consisting of a variable @cindex @w{variable} (section @pxref{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. @cindex @w{unbound} @format @t{(define x 28) x ==> 28 } @end format @end deffn @node Literal expressions, Procedure calls, Variable references, Primitive expression types @subsection Literal expressions @deffn {essential syntax} quote @r{} @deffnx {essential syntax} @t{'}@r{} @deffnx {essential syntax} @r{} @samp{(quote @r{})} evaluates to @r{}. @cindex @w{'} @r{} may be any external representation of a Scheme object (see section @pxref{External representations}). This notation is used to include literal constants in Scheme code. @format @t{ (quote a) ==> a (quote #(a b c)) ==> #(a b c) (quote (+ 1 2)) ==> (+ 1 2) } @end format @samp{(quote @r{})} may be abbreviated as @t{'}@r{}. The two notations are equivalent in all respects. @format @t{'a ==> a '#(a b c) ==> #(a b c) '(+ 1 2) ==> (+ 1 2) '(quote a) ==> (quote a) ''a ==> (quote a) } @end format Numeric constants, string constants, character constants, and boolean constants evaluate ``to themselves''; they need not be quoted. @format @t{'"abc" ==> "abc" "abc" ==> "abc" '145932 ==> 145932 145932 ==> 145932 '#t ==> #t #t ==> #t } @end format It is an error to alter a constant (i.e. the value of a literal expression) using a mutation procedure like @code{set-car!} or @vindex @w{set-car!} @code{string-set!}. @vindex @w{string-set!} @end deffn @node Procedure calls, lambda expressions, Literal expressions, Primitive expression types @subsection Procedure calls @deffn {essential syntax} (@r{} @r{} @dots{},) 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. @cindex @w{procedure call} @cindex @w{call} @format @t{ (+ 3 4) ==> 7 ((if #f + *) 3 4) ==> 12 } @end format 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 @code{+} @vindex @w{+} and @code{*}. New procedures are created by evaluating lambda expressions @vindex @w{*} (see section @pxref{lambda expressions}). @ignore todo At Friedman's reuest, flushed mention of other ways. @end ignore @c or definitions (see section~\ref{define}). Procedure calls are also called @emph{combinations}. @cindex @w{combination} @quotation @emph{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. @ignore todo Clinger: maybe say ``This is a major source of simplicity.'' @end ignore @end quotation @quotation @emph{Note:} In many dialects of Lisp, the empty combination, @t{()}, is a legitimate expression. In Scheme, combinations must have at least one subexpression, so @t{()} is not a syntactically valid expression. @ignore todo Dybvig: ``it should be obvious from the syntax.'' @end ignore @end quotation @ignore todo Freeman: I think an explanation as to why evaluation order is not specified should be included. It should not include any reference to parallel evaluation. Does any existing compiler generate better code because the evaluation order is unspecified? @end ignore @end deffn @node lambda expressions, Conditionals, Procedure calls, Primitive expression types @subsection lambda expressions @deffn {essential syntax} lambda @r{} @r{} @emph{Syntax:} @r{} should be a formal arguments list as described below, and @r{} should be a sequence of one or more expressions. @emph{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. @format @t{(lambda (x) (+ x x)) ==> @emph{}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 } @end format @r{} should have one of the following forms: @itemize @bullet @item @t{(@r{} @dots{},)}: 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. @item @r{}: 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 @r{}. @item @t{(@r{} @dots{}, @r{} @b{.} @r{})}: 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. @end itemize @format @t{((lambda x x) 3 4 5 6) ==> (3 4 5 6) ((lambda (x y . z) z) 3 4 5 6) ==> (5 6) } @end format @end deffn @node Conditionals, Assignments, lambda expressions, Primitive expression types @subsection Conditionals @deffn {essential syntax} if @r{} @r{} @r{} @deffnx {syntax} if @r{} @r{} @c \/ if hyper = italic @emph{Syntax:} @r{}, @r{}, and @r{} may be arbitrary expressions. @emph{Semantics:} An @code{if} expression is evaluated as follows: first, @vindex @w{if} @r{} is evaluated. If it yields a true value (see @cindex @w{true} section @pxref{Booleans}), then @r{} is evaluated and its value is returned. Otherwise @r{} is evaluated and its value is returned. If @r{} yields a false value and no @r{} is specified, then the result of the expression is unspecified. @format @t{(if (> 3 2) 'yes 'no) ==> yes (if (> 2 3) 'yes 'no) ==> no (if (> 3 2) (- 3 2) (+ 3 2)) ==> 1 } @end format @end deffn @node Assignments, , Conditionals, Primitive expression types @subsection Assignments @deffn {essential syntax} set! @r{} @r{} @r{} is evaluated, and the resulting value is stored in the location to which @r{} is bound. @r{} must be bound either in some region enclosing the @code{set!} expression @vindex @w{set!} @cindex @w{region} or at top level. The result of the @code{set!} expression is @vindex @w{set!} unspecified. @format @t{(define x 2) (+ x 1) ==> 3 (set! x 4) ==> @emph{unspecified} (+ x 1) ==> 5 } @end format @end deffn @node Derived expression types, , Primitive expression types, Expressions @section Derived expression types @menu * Conditional:: * Binding constructs:: * Sequencing:: * Iteration:: * Delayed evaluation:: * Quasiquotation:: @end menu For reference purposes, section @ref{Derived expression type} gives rewrite rules that will convert constructs described in this section into the primitive constructs described in the previous section. @node Conditional, Binding constructs, Derived expression types, Derived expression types @subsection Conditionals @deffn {essential syntax} cond @dots{}, @emph{Syntax:} Each @r{} should be of the form @format @t{(@r{} @r{} @dots{},) } @end format where @r{} is any expression. The last @r{} may be an ``else clause,'' which has the form @format @t{(else @r{} @r{} @dots{},)@r{.} } @end format @cindex @w{else} @cindex @w{=>} @emph{Semantics:} A @code{cond} expression is evaluated by evaluating the @r{} @vindex @w{cond} expressions of successive @r{}s in order until one of them evaluates to a true value (see @cindex @w{true} section @pxref{Booleans}). When a @r{} evaluates to a true value, then the remaining @r{}s in its @r{} are evaluated in order, and the result of the last @r{} in the @r{} is returned as the result of the entire @code{cond} @vindex @w{cond} expression. If the selected @r{} contains only the @r{} and no @r{}s, then the value of the @r{} is returned as the result. If all @r{}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 @r{}s are evaluated, and the value of the last one is returned. @format @t{(cond ((> 3 2) 'greater) ((< 3 2) 'less)) ==> greater (cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) ==> equal } @end format Some implementations support an alternative @r{} syntax, @t{(@r{} => @r{})}, where @r{} is an expression. If @r{} evaluates to a true value, then @r{} is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the @r{}. @format @t{(cond ((assv 'b '((a 1) (b 2))) => cadr) (else #f)) ==> 2 } @end format @end deffn @deffn {syntax} case @r{} @dots{}, @emph{Syntax:} @r{} may be any expression. Each @r{} should have the form @format @t{((@r{} @dots{},) @r{} @r{} @dots{},)@r{,} } @end format where each @r{} is an external representation of some object. All the @r{}s must be distinct. The last @r{} may be an ``else clause,'' which has the form @format @t{(else @r{} @r{} @dots{},)@r{.} } @end format @vindex else @emph{Semantics:} A @code{case} expression is evaluated as follows. @r{} is @vindex @w{case} evaluated and its result is compared against each @r{}. If the result of evaluating @r{} is equivalent (in the sense of @code{eqv?}; see section @pxref{Equivalence predicates}) to a @r{}, then the @vindex @w{eqv?} expressions in the corresponding @r{} are evaluated from left to right and the result of the last expression in the @r{} is returned as the result of the @code{case} expression. If the result of @vindex @w{case} evaluating @r{} is different from every @r{}, then if there is an else clause its expressions are evaluated and the result of the last is the result of the @code{case} expression; otherwise @vindex @w{case} the result of the @code{case} expression is unspecified. @vindex @w{case} @format @t{(case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) ==> composite (case (car '(c d)) ((a) 'a) ((b) 'b)) ==> @emph{unspecified} (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) ==> consonant } @end format @end deffn @deffn {syntax} and @dots{}, The @r{} expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see section @pxref{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{#t} is returned. @format @t{(and (= 2 2) (> 2 1)) ==> #t (and (= 2 2) (< 2 1)) ==> #f (and 1 2 'c '(f g)) ==> (f g) (and) ==> #t } @end format @end deffn @deffn {syntax} or @dots{}, The @r{} expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see section @pxref{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 @t{#f} is returned. @format @t{(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) } @end format @end deffn @node Binding constructs, Sequencing, Conditional, Derived expression types @subsection Binding constructs The three binding constructs @code{let}, @code{let*}, and @code{letrec} @vindex @w{letrec} @vindex @w{let*} @vindex @w{let} give Scheme a block structure, like Algol 60. The syntax of the three constructs is identical, but they differ in the regions they establish @cindex @w{region} for their variable bindings. In a @code{let} expression, the initial @vindex @w{let} values are computed before any of the variables become bound; in a @code{let*} expression, the bindings and evaluations are performed @vindex @w{let*} sequentially; while in a @code{letrec} expression, the bindings are in @vindex @w{letrec} effect while their initial values are being computed, thus allowing mutually recursive definitions. @deffn {essential syntax} let @r{} @r{} @emph{Syntax:} @r{} should have the form @format @t{((@r{} @r{}) @dots{},)@r{,} } @end format where each @r{} is an expression, and @r{} should be a sequence of one or more expressions. @emph{Semantics:} The @r{}s are evaluated in the current environment (in some unspecified order), the @r{}s are bound to fresh locations holding the results, the @r{} is evaluated in the extended environment, and the value of the last expression of @r{} is returned. Each binding of a @r{} has @r{} as its region. @cindex @w{region} @format @t{(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 } @end format See also named @code{let}, section @ref{Iteration}. @vindex @w{let} @end deffn @deffn {syntax} let* @r{} @r{} @emph{Syntax:} @r{} should have the form @format @t{((@r{} @r{}) @dots{},)@r{,} } @end format and @r{} should be a sequence of one or more expressions. @emph{Semantics:} @code{Let*} is similar to @code{let}, but the bindings are performed @vindex @w{let} @vindex @w{Let*} sequentially from left to right, and the region of a binding indicated @cindex @w{region} by @samp{(@r{} @r{})} is that part of the @code{let*} @vindex @w{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. @format @t{(let* ((x 1) (y (+ x 1))) y) ==> 2 } @end format @end deffn @deffn {essential syntax} letrec @r{} @r{} @emph{Syntax:} @r{} should have the form @format @t{((@r{} @r{}) @dots{},)@r{,} } @end format and @r{} should be a sequence of one or more expressions. @emph{Semantics:} The @r{}s are bound to fresh locations holding undefined values, the @r{}s are evaluated in the resulting environment (in some unspecified order), each @r{} is assigned to the result of the corresponding @r{}, the @r{} is evaluated in the resulting environment, and the value of the last expression in @r{} is returned. Each binding of a @r{} has the entire @code{letrec} expression as its region, making it possible to @cindex @w{region} @vindex @w{letrec} define mutually recursive procedures. @format @t{@c (letrec ((x 2) (y 3)) @c (letrec ((foo (lambda (z) (+ x y z))) (x 7)) @c (foo 4))) \ev 14 (letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88)) ==> #t } @end format One restriction on @code{letrec} is very important: it must be possible @vindex @w{letrec} to evaluate each @r{} without referring to the value of any @r{}. If this restriction is violated, then the effect is undefined, and an error may be signalled during evaluation of the @r{}s. The restriction is necessary because Scheme passes arguments by value rather than by name. In the most common uses of @code{letrec}, all the @r{}s are lambda expressions and the @vindex @w{letrec} restriction is satisfied automatically. @c \todo{use or uses? --- Jinx.} @end deffn @node Sequencing, Iteration, Binding constructs, Derived expression types @subsection Sequencing @deffn {essential syntax} begin @dots{}, The @r{}s are evaluated sequentially from left to right, and the value of the last @r{} is returned. This expression type is used to sequence side effects such as input and output. @format @t{(begin (set! x 5) (+ x 1)) ==> 6 (begin (display "4 plus 1 equals ") (display (+ 4 1))) ==> @emph{unspecified} @emph{and prints} 4 plus 1 equals 5 } @end format @quotation @emph{Note:} [SICP] uses the keyword @code{sequence} instead of @code{begin}. @vindex @w{begin} @vindex @w{sequence} @end quotation @end deffn @node Iteration, Delayed evaluation, Sequencing, Derived expression types @subsection Iteration @c \unsection @noindent @deffn {syntax} (do ((@r{} @r{} @r{}) @t{ @dots{},) (@r{} @r{} @dots{},) @r{} @dots{},)} @cindex @w{do} @code{Do} is an iteration construct. It specifies a set of variables to @vindex @w{Do} 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. @code{Do} expressions are evaluated as follows: @vindex @w{Do} The @r{} expressions are evaluated (in some unspecified order), the @r{}s are bound to fresh locations, the results of the @r{} expressions are stored in the bindings of the @r{}s, and then the iteration phase begins. Each iteration begins by evaluating @r{}; if the result is false (see section @pxref{Booleans}), then the @r{} expressions are evaluated in order for effect, the @r{} expressions are evaluated in some unspecified order, the @r{}s are bound to fresh locations, the results of the @r{}s are stored in the bindings of the @r{}s, and the next iteration begins. If @r{} evaluates to a true value, then the @r{}s are evaluated from left to right and the value of the last @r{} is returned as the value of the @code{do} @vindex @w{do} expression. If no @r{}s are present, then the value of the @code{do} expression is unspecified. @vindex @w{do} The region of the binding of a @r{} @cindex @w{region} consists of the entire @code{do} expression except for the @r{}s. @vindex @w{do} A @r{} may be omitted, in which case the effect is the same as if @samp{(@r{} @r{} @r{})} had been written instead of @samp{(@r{} @r{})}. @format @t{(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 } @end format @c \end{entry} @deffnx {syntax} let @r{} @r{} @r{} Some implementations of Scheme permit a variant on the syntax of @code{let} called ``named @code{let}'' which provides a more general @vindex @w{let} looping construct than @code{do}, and may also be used to express @vindex @w{do} recursions. Named @code{let} has the same syntax and semantics as ordinary @code{let} @vindex @w{let} except that @r{} is bound within @r{} to a procedure whose formal arguments are the bound variables and whose body is @r{}. Thus the execution of @r{} may be repeated by invoking the procedure named by @r{}. @c | <-- right margin @format @t{(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)) } @end format @end deffn @node Delayed evaluation, Quasiquotation, Iteration, Derived expression types @subsection Delayed evaluation @deffn {syntax} delay @r{} @ignore todo Fix. @end ignore The @code{delay} construct is used together with the procedure @code{force} to @vindex @w{force} @vindex @w{delay} implement @dfn{lazy evaluation} or @dfn{call by need}. @cindex @w{call by need} @cindex @w{lazy evaluation} @t{(delay @r{})} returns an object called a @dfn{promise} which at some point in the future may be asked (by @cindex @w{promise} the @code{force} procedure) @vindex @w{force} @ignore todo Bartley's white lie; OK? @end ignore to evaluate @r{} and deliver the resulting value. See the description of @code{force} (section @pxref{Control features}) for a @vindex @w{force} complete description of @code{delay}. @vindex @w{delay} @end deffn @node Quasiquotation, , Delayed evaluation, Derived expression types @subsection Quasiquotation @deffn {syntax} quasiquote @r{