Revised5 Report on the Algorithmic Language Scheme

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

Dedicated to the Memory of Robert Hieb

20 February 1998 CONTENTS Introduction ...... 2 SUMMARY 1 Overview of Scheme ...... 3 1.1 Semantics ...... 3 The report gives a defining description of the program- 1.2 Syntax ...... 3 ming language Scheme. Scheme is a statically scoped and 1.3 Notation and terminology ...... 3 properly tail-recursive dialect of the Lisp programming 2 Lexical conventions ...... 5 language invented by Guy Lewis Steele Jr. and Gerald 2.1 Identifiers ...... 5 Jay Sussman. It was designed to have an exceptionally 2.2 Whitespace and comments ...... 5 clear and simple semantics and few different ways to form 2.3 Other notations ...... 5 expressions. A wide variety of programming paradigms, in- 3 Basic concepts ...... 6 3.1 Variables, syntactic keywords, and regions ...... 6 cluding imperative, functional, and message passing styles, 3.2 Disjointness of types ...... 6 find convenient expression in Scheme. 3.3 External representations ...... 6 The introduction offers a brief history of the language and 3.4 Storage model ...... 7 of the report. 3.5 Proper tail recursion ...... 7 4 Expressions ...... 8 The first three chapters present the fundamental ideas of 4.1 Primitive expression types ...... 8 the language and describe the notational conventions used 4.2 Derived expression types ...... 10 for describing the language and for writing programs in the 4.3 Macros ...... 13 language. 5 Program structure ...... 16 5.1 Programs ...... 16 Chapters 4 and 5 describe the syntax and semantics of 5.2 Definitions ...... 16 expressions, programs, and definitions. 5.3 Syntax definitions ...... 17 Chapter 6 describes Scheme’s built-in procedures, which 6 Standard procedures ...... 17 include all of the language’s data manipulation and in- 6.1 Equivalence predicates ...... 17 put/output primitives. 6.2 Numbers ...... 19 6.3 Other data types ...... 25 Chapter 7 provides a formal syntax for Scheme written in 6.4 Control features ...... 31 extended BNF, along with a formal denotational semantics. 6.5 Eval ...... 35 An example of the use of the language follows the formal 6.6 Input and output ...... 35 syntax and semantics. 7 Formal syntax and semantics ...... 38 7.1 Formal syntax ...... 38 The report concludes with a list of references and an al- 7.2 Formal semantics ...... 40 phabetic index. 7.3 Derived expression types ...... 43 Notes ...... 45 Additional material ...... 45 Example ...... 45 References ...... 46 Alphabetic index of definitions of concepts, keywords, and procedures ...... 48 2 Revised5 Scheme INTRODUCTION

Programming languages should be designed not by piling munity, and so we grant permission to copy it in whole or in feature on top of feature, but by removing the weaknesses part without fee. In particular, we encourage implementors and restrictions that make additional features appear nec- of Scheme to use this report as a starting point for manuals essary. Scheme demonstrates that a very small number of and other documentation, modifying it as necessary. rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient Acknowledgements programming language that is flexible enough to support most of the major programming paradigms in use today. We would like to thank the following people for their Scheme was one of the first programming languages to in- help: Alan Bawden, Michael Blair, George Carrette, Andy corporate first class procedures as in the , Cromarty, Pavel Curtis, Jeff Dalton, Olivier Danvy, Ken thereby proving the usefulness of static rules and Dickey, Bruce Duba, Marc Feeley, Andy Freeman, Richard block structure in a dynamically typed language. Scheme Gabriel, Yekta Gursel,¨ Ken Haase, Robert Hieb, Paul was the first major dialect of Lisp to distinguish procedures Hudak, Morry Katz, Chris Lindblad, Mark Meyer, Jim from lambda expressions and symbols, to use a single lex- Miller, Jim Philbin, John Ramsdell, Mike Shaff, Jonathan ical environment for all variables, and to evaluate the op- Shapiro, Julie Sussman, Perry Wagle, Daniel Weise, Henry erator position of a procedure call in the same way as an Wu, and Ozan Yigit. We thank Carol Fessenden, Daniel operand position. By relying entirely on procedure calls Friedman, and Christopher Haynes for permission to use to express iteration, Scheme emphasized the fact that tail- text from the Scheme 311 version 4 reference manual. recursive procedure calls are essentially goto’s that pass We thank Texas Instruments, Inc. for permission to use arguments. Scheme was the first widely used program- text from the TI Scheme Language Reference Manual[30]. ming language to embrace first class escape procedures, We gladly acknowledge the influence of manuals for MIT from which all previously known sequential control struc- Scheme[17], T[22], Scheme 84[11],Common Lisp[27], and tures can be synthesized. A subsequent version of Scheme Algol 60[18]. introduced the concept of exact and inexact numbers, an We also thank Betty Dexter for the extreme effort she put extension of Common Lisp’s generic arithmetic. More re- into setting this report in T X, and Donald Knuth for de- cently, Scheme became the first programming language to E signing the program that caused her troubles. support hygienic macros, which permit the syntax of a block-structured language to be extended in a consistent The Artificial Intelligence Laboratory of the Massachusetts and reliable manner. Institute of Technology, the Computer Science Department of Indiana University, the Computer and Information Sci- ences Department of the University of Oregon, and the Background NEC Research Institute supported the preparation of this The first description of Scheme was written in 1975 [28]. A report. Support for the MIT work was provided in part by revised report [25] appeared in 1978, which described the the Advanced Research Projects Agency of the Department evolution of the language as its MIT implementation was of Defense under Office of Naval Research contract N00014- upgraded to support an innovative compiler [26]. Three 80-C-0505. Support for the Indiana University work was distinct projects began in 1981 and 1982 to use variants provided by NSF grants NCS 83-04567 and NCS 83-03325. of Scheme for courses at MIT, Yale, and Indiana Univer- sity [21, 17, 10]. An introductory computer science text- book using Scheme was published in 1984 [1]. As Scheme became more widespread, local dialects be- gan to diverge until students and researchers occasion- ally found it difficult to understand code written at other sites. Fifteen representatives of the major implementations of Scheme therefore met in October 1984 to work toward a better and more widely accepted standard for Scheme. Their report [4] was published at MIT and Indiana Univer- sity in the summer of 1985. Further revision took place in the spring of 1986 [23], and in the spring of 1988 [6]. The present report reflects further revisions agreed upon in a meeting at Xerox PARC in June 1992.

We intend this report to belong to the entire Scheme com- 1. Overview of Scheme 3 DESCRIPTION OF THE LANGUAGE

1. Overview of Scheme ML, C, and APL are three other languages that always pass arguments by value. This is distinct from the lazy- 1.1. Semantics evaluation semantics of Haskell, or the call-by-name se- mantics of Algol 60, where an argument expression is not This section gives an overview of Scheme’s semantics. A evaluated unless its value is needed by the procedure. detailed informal semantics is the subject of chapters 3 through 6. For reference purposes, section 7.2 provides a Scheme’s model of arithmetic is designed to remain as in- formal semantics of Scheme. dependent as possible of the particular ways in which num- bers are represented within a computer. In Scheme, every Following Algol, Scheme is a statically scoped program- integer is a rational number, every rational is a real, and ming language. Each use of a variable is associated with a every real is a complex number. Thus the distinction be- lexically apparent binding of that variable. tween integer and real arithmetic, so important to many Scheme has latent as opposed to manifest types. Types programming languages, does not appear in Scheme. In its are associated with values (also called objects) rather than place is a distinction between exact arithmetic, which cor- with variables. (Some authors refer to languages with responds to the mathematical ideal, and inexact arithmetic latent types as weakly typed or dynamically typed lan- on approximations. As in Common Lisp, exact arithmetic guages.) Other languages with latent types are APL, is not limited to integers. Snobol, and other dialects of Lisp. Languages with mani- fest types (sometimes referred to as strongly typed or stat- ically typed languages) include Algol 60, Pascal, and C. 1.2. Syntax All objects created in the course of a Scheme computation, Scheme, like most dialects of Lisp, employs a fully paren- including procedures and continuations, have unlimited ex- thesized prefix notation for programs and (other) data; the tent. No Scheme object is ever destroyed. The reason that grammar of Scheme generates a sublanguage of the lan- implementations of Scheme do not (usually!) run out of guage used for data. An important consequence of this sim- storage is that they are permitted to reclaim the storage ple, uniform representation is the susceptibility of Scheme occupied by an object if they can prove that the object programs and data to uniform treatment by other Scheme cannot possibly matter to any future computation. Other programs. For example, the eval procedure evaluates a languages in which most objects have unlimited extent in- Scheme program expressed as data. clude APL and other Lisp dialects. The read procedure performs syntactic as well as lexical Implementations of Scheme are required to be properly decomposition of the data it reads. The read procedure tail-recursive. This allows the execution of an iterative parses its input as data (section 7.1.2), not as program. computation in constant space, even if the iterative compu- tation is described by a syntactically recursive procedure. The formal syntax of Scheme is described in section 7.1. Thus with a properly tail-recursive implementation, iter- ation can be expressed using the ordinary procedure-call mechanics, so that special iteration constructs are useful 1.3. Notation and terminology only as . See section 3.5. 1.3.1. Primitive, library, and optional features Scheme procedures are objects in their own right. Pro- cedures can be created dynamically, stored in data struc- It is required that every implementation of Scheme support tures, returned as results of procedures, and so on. Other all features that are not marked as being optional. Imple- languages with these properties include Common Lisp and mentations are free to omit optional features of Scheme ML. or to add extensions, provided the extensions are not in One distinguishing feature of Scheme is that continuations, conflict with the language reported here. In particular, which in most other languages only operate behind the implementations must support portable code by providing scenes, also have “first-class” status. Continuations are a syntactic mode that preempts no lexical conventions of useful for implementing a wide variety of advanced control this report. constructs, including non-local exits, backtracking, and To aid in understanding and implementing Scheme, some coroutines. See section 6.4. features are marked as library. These can be easily imple- Arguments to Scheme procedures are always passed by mented in terms of the other, primitive, features. They are value, which means that the actual argument expressions redundant in the strict sense of the word, but they capture are evaluated before the procedure gains control, whether common patterns of usage, and are therefore provided as the procedure needs the result of the evaluation or not. convenient abbreviations. 4 Revised5 Scheme

1.3.2. Error situations and unspecified behavior hthing1i . . .

When speaking of an error situation, this report uses the indicates zero or more occurrences of a hthingi, and phrase “an error is signalled” to indicate that implemen- hthing1i hthing2i . . . tations must detect and report the error. If such wording indicates one or more occurrences of a hthingi. does not appear in the discussion of an error, then imple- mentations are not required to detect or report the error, If category is “procedure”, then the entry describes a pro- though they are encouraged to do so. An error situation cedure, and the header line gives a template for a call to the that implementations are not required to detect is usually procedure. Argument names in the template are italicized . referred to simply as “an error.” Thus the header line For example, it is an error for a procedure to be passed an (vector-ref vector k) procedure argument that the procedure is not explicitly specified to indicates that the built-in procedure vector-ref takes two handle, even though such domain errors are seldom men- arguments, a vector vector and an exact non-negative in- tioned in this report. Implementations may extend a pro- teger k (see below). The header lines cedure’s domain of definition to include such arguments. (make-vector k) procedure This report uses the phrase “may report a violation of an (make-vector k fill) procedure implementation restriction” to indicate circumstances un- indicate that the make-vector procedure must be defined der which an implementation is permitted to report that to take either one or two arguments. it is unable to continue execution of a correct program be- cause of some restriction imposed by the implementation. It is an error for an operation to be presented with an ar- Implementation restrictions are of course discouraged, but gument that it is not specified to handle. For succinctness, implementations are encouraged to report violations of im- we follow the convention that if an argument name is also plementation restrictions. the name of a type listed in section 3.2, then that argu- ment must be of the named type. For example, the header For example, an implementation may report a violation of line for vector-ref given above dictates that the first ar- an implementation restriction if it does not have enough gument to vector-ref must be a vector. The following storage to run a program. naming conventions also imply type restrictions: If the value of an expression is said to be “unspecified,” then the expression must evaluate to some object without obj any object signalling an error, but the value depends on the imple- list, list1, . . . listj, . . . list (see section 6.3.2) mentation; this report explicitly does not say what value z, z1, . . . zj, . . . complex number should be returned. x, x1, . . . xj , . . . real number y, y1, . . . yj , . . . real number q, q1, . . . qj , . . . rational number 1.3.3. Entry format n, n1, . . . nj, . . . integer k, k , . . . k , . . . exact non-negative integer Chapters 4 and 6 are organized into entries. Each entry de- 1 j scribes one language feature or a group of related features, where a feature is either a syntactic construct or a built-in 1.3.4. Evaluation examples procedure. An entry begins with one or more header lines of the form The symbol “=⇒” used in program examples should be read “evaluates to.” For example, template category (* 5 8) =⇒ 40 for required, primitive features, or means that the expression (* 5 8) evaluates to the ob- template qualifier category ject 40. Or, more precisely: the expression given by the where qualifier is either “library” or “optional” as defined sequence of characters “(* 5 8)” evaluates, in the initial in section 1.3.1. environment, to an object that may be represented exter- If category is “syntax”, the entry describes an expression nally by the sequence of characters “40”. See section 3.3 type, and the template gives the syntax of the expression for a discussion of external representations of objects. type. Components of expressions are designated by syn- tactic variables, which are written using angle brackets, 1.3.5. Naming conventions for example, hexpressioni, hvariablei. Syntactic variables should be understood to denote segments of program text; By convention, the names of procedures that always return for example, hexpressioni stands for any string of charac- a boolean value usually end in “?”. Such procedures are ters which is a syntactically valid expression. The notation called predicates. 2. Lexical conventions 5

By convention, the names of procedures that store values 2.2. Whitespace and comments into previously allocated locations (see section 3.4) usually end in “!”. Such procedures are called mutation proce- Whitespace characters are spaces and newlines. (Imple- dures. By convention, the value returned by a mutation mentations typically provide additional whitespace char- procedure is unspecified. acters such as tab or page break.) Whitespace is used for By convention, “->” appears within the names of proce- improved readability and as necessary to separate tokens dures that take an object of one type and return an anal- from each other, a token being an indivisible lexical unit ogous object of another type. For example, list->vector such as an identifier or number, but is otherwise insignifi- takes a list and returns a vector whose elements are the cant. Whitespace may occur between any two tokens, but same as those of the list. not within a token. Whitespace may also occur inside a string, where it is significant. A semicolon (;) indicates the start of a comment. The 2. Lexical conventions comment continues to the end of the line on which the This section gives an informal account of some of the lexical semicolon appears. Comments are invisible to Scheme, but conventions used in writing Scheme programs. For a formal the end of the line is visible as whitespace. This prevents a syntax of Scheme, see section 7.1. comment from appearing in the middle of an identifier or number. Upper and lower case forms of a letter are never distin- guished except within character and string constants. For ;;; The FACT procedure computes the factorial example, Foo is the same identifier as FOO, and #x1AB is ;;; of a non-negative integer. the same number as #X1ab. (define fact (lambda (n) (if (= n 0) 1 ;Base case: return 1 2.1. Identifiers (* n (fact (- n 1))))))

Most identifiers allowed by other programming languages are also acceptable to Scheme. The precise rules for form- 2.3. Other notations ing identifiers vary among implementations of Scheme, but in all implementations a sequence of letters, digits, and “ex- For a description of the notations used for numbers, see tended alphabetic characters” that begins with a character section 6.2. that cannot begin a number is an identifier. In addition, +, -, and ... are identifiers. Here are some examples of identifiers: . + - These are used in numbers, and may also occur anywhere in an identifier except as the first charac- lambda q ter. A delimited plus or minus sign by itself is also an list->vector soup identifier. A delimited period (not occurring within a + V17a number or identifier) is used in the notation for pairs <=? a34kTMNs (section 6.3.2), and to indicate a rest-parameter in a the-word-recursion-has-many-meanings formal parameter list (section 4.1.4). A delimited se- quence of three successive periods is also an identifier. Extended alphabetic characters may be used within iden- tifiers as if they were letters. The following are extended ( ) Parentheses are used for grouping and to notate lists alphabetic characters: (section 6.3.2).

! $ % & * + - . / : < = > ? @ ^ _ ~ ’ The single quote character is used to indicate literal data (section 4.1.2). See section 7.1.1 for a formal syntax of identifiers. Identifiers have two uses within Scheme programs: ` The backquote character is used to indicate almost- constant data (section 4.2.6).

• Any identifier may be used as a variable or as a syn- , ,@ The character comma and the sequence comma at- tactic keyword (see sections 3.1 and 4.3). sign are used in conjunction with backquote (sec- tion 4.2.6). • When an identifier appears as a literal or within a literal (see section 4.1.2), it is being used to denote a " The double quote character is used to delimit strings symbol (see section 6.3.3). (section 6.3.5). 6 Revised5 Scheme

\ Backslash is used in the syntax for character constants The region is determined by the particular binding con- (section 6.3.4) and as an escape character within string struct that establishes the binding; if the binding is estab- constants (section 6.3.5). lished by a lambda expression, for example, then its region is the entire lambda expression. Every mention of an iden- [ ] { } | Left and right square brackets and curly braces tifier refers to the binding of the identifier that established and vertical bar are reserved for possible future exten- the innermost of the regions containing the use. If there is sions to the language. no binding of the identifier whose region contains the use, # Sharp sign is used for a variety of purposes depending then the use refers to the binding for the variable in the on the character that immediately follows it: top level environment, if any (chapters 4 and 6); if there is no binding for the identifier, it is said to be unbound. #t #f These are the boolean constants (section 6.3.1). #\ This introduces a character constant (section 6.3.4). 3.2. Disjointness of types #( This introduces a vector constant (section 6.3.6). Vec- tor constants are terminated by ) . No object satisfies more than one of the following predi- cates: #e #i #b #o #d #x These are used in the notation for numbers (section 6.2.4). boolean? pair? symbol? number? char? string? vector? port? 3. Basic concepts procedure? 3.1. Variables, syntactic keywords, and re- These predicates define the types boolean, pair, symbol, gions number, char (or character), string, vector, port, and pro- cedure. The empty list is a special object of its own type; An identifier may name a type of syntax, or it may name it satisfies none of the above predicates. a location where a value can be stored. An identifier that Although there is a separate boolean type, any Scheme names a type of syntax is called a syntactic keyword and is value can be used as a boolean value for the purpose of a said to be bound to that syntax. An identifier that names conditional test. As explained in section 6.3.1, all values a location is called a variable and is said to be bound to count as true in such a test except for #f. This report uses that location. The set of all visible bindings in effect at the word “true” to refer to any Scheme value except #f, some point in a program is known as the environment in and the word “false” to refer to #f. 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 3.3. External representations to name the value or to be bound to the value. This is not quite accurate, but confusion rarely results from this An important concept in Scheme (and Lisp) is that of the practice. external representation of an object as a sequence of char- Certain expression types are used to create new kinds of acters. For example, an external representation of the inte- syntax and bind syntactic keywords to those new syntaxes, ger 28 is the sequence of characters “28”, and an external while other expression types create new locations and bind representation of a list consisting of the integers 8 and 13 variables to those locations. These expression types are is the sequence of characters “(8 13)”. called binding constructs. Those that bind syntactic key- The external representation of an object is not neces- words are listed in section 4.3. The most fundamental of sarily unique. The integer 28 also has representations the variable binding constructs is the lambda expression, “#e28.000” and “#x1c”, and the list in the previous para- because all other variable binding constructs can be ex- graph also has the representations “( 08 13 )” and “(8 plained in terms of lambda expressions. The other variable . (13 . ()))” (see section 6.3.2). binding constructs are let, let*, letrec, and do expres- sions (see sections 4.1.4, 4.2.2, and 4.2.4). Many objects have standard external representations, but some, such as procedures, do not have standard represen- Like Algol and Pascal, and unlike most other dialects of tations (although particular implementations may define Lisp except for Common Lisp, Scheme is a statically scoped representations for them). language with block structure. To each place where an identifier is bound in a program there corresponds a region An external representation may be written in a program to of the program text within which the binding is visible. obtain the corresponding object (see quote, section 4.1.2). 3. Basic concepts 7

External representations can also be used for input and created by the other procedures listed in this report are output. The procedure read (section 6.6.2) parses external mutable. It is an error to attempt to store a new value representations, and the procedure write (section 6.6.3) into a location that is denoted by an immutable object. generates them. Together, they provide an elegant and powerful input/output facility. 3.5. Proper tail recursion Note that the sequence of characters “(+ 2 6)” is not an external representation of the integer 8, even though it is an Implementations of Scheme are required to be properly tail- expression evaluating to the integer 8; rather, it is an exter- recursive. Procedure calls that occur in certain syntactic nal representation of a three-element list, the elements of contexts defined below are ‘tail calls’. A Scheme imple- which are the symbol + and the integers 2 and 6. Scheme’s mentation is properly tail-recursive if it supports an un- syntax has the property that any sequence of characters bounded number of active tail calls. A call is active if that is an expression is also the external representation of the called procedure may still return. Note that this in- some object. This can lead to confusion, since it may not cludes calls that may be returned from either by the cur- be obvious out of context whether a given sequence of char- rent continuation or by continuations captured earlier by acters is intended to denote data or program, but it is also call-with-current-continuation that are later invoked. a source of power, since it facilitates writing programs such In the absence of captured continuations, calls could return as interpreters and compilers that treat programs as data at most once and the active calls would be those that had (or vice versa). not yet returned. A formal definition of proper tail recur- The syntax of external representations of various kinds of sion can be found in [8]. objects accompanies the description of the primitives for Rationale: manipulating the objects in the appropriate sections of chapter 6. Intuitively, no space is needed for an active tail call because the continuation that is used in the tail call has the same semantics as the continuation passed to the procedure containing the call. Although an improper implementation might use a new con- 3.4. Storage model tinuation in the call, a return to this new continuation would be followed immediately by a return to the continuation passed Variables and objects such as pairs, vectors, and strings to the procedure. A properly tail-recursive implementation re- implicitly denote locations or sequences of locations. A turns to that continuation directly. string, for example, denotes as many locations as there Proper tail recursion was one of the central ideas in Steele and are characters in the string. (These locations need not Sussman’s original version of Scheme. Their first Scheme in- correspond to a full machine word.) A new value may be terpreter implemented both functions and actors. Control flow stored into one of these locations using the string-set! was expressed using actors, which differed from functions in procedure, but the string continues to denote the same that they passed their results on to another actor instead of locations as before. returning to a caller. In the terminology of this section, each actor finished with a tail call to another actor. An object fetched from a location, by a variable reference or by a procedure such as car, vector-ref, or string-ref, Steele and Sussman later observed that in their interpreter the code for dealing with actors was identical to that for functions is equivalent in the sense of eqv? (section 6.1) to the object and thus there was no need to include both in the language. last stored in the location before the fetch. A tail call is a procedure call that occurs in a tail con- Every location is marked to show whether it is in use. No text. Tail contexts are defined inductively. Note that a tail variable or object ever refers to a location that is not in use. context is always determined with respect to a particular Whenever this report speaks of storage being allocated for lambda expression. a variable or object, what is meant is that an appropriate number of locations are chosen from the set of locations • The last expression within the body of a lambda ex- that are not in use, and the chosen locations are marked pression, shown as htail expressioni below, occurs in a to indicate that they are now in use before the variable or tail context. object is made to denote them. In many systems it is desirable for constants (i.e. the val- (lambda hformalsi ues of literal expressions) to reside in read-only-memory. hdefinitioni* hexpressioni* htail expressioni) To express this, it is convenient to imagine that every object that denotes locations is associated with a flag telling whether that object is mutable or immutable. In • If one of the following expressions is in a tail context, such systems literal constants and the strings returned by then the subexpressions shown as htail expressioni are symbol->string are immutable objects, while all objects in a tail context. These were derived from rules in 8 Revised5 Scheme

the grammar given in chapter 7 by replacing some oc- In the following example the only tail call is the call to f. currences of hexpressioni with htail expressioni. Only None of the calls to g or h are tail calls. The reference to those rules that contain tail contexts are shown here. x is in a tail context, but it is not a call and thus is not a tail call.

(if hexpressioni htail expressioni htail expressioni) (lambda () (if (g) (if hexpressioni htail expressioni) (let ((x (h)))

+ x) (cond hcond clausei ) (and (g) (f)))) (cond hcond clausei* (else htail sequencei))

(case hexpressioni Note: Implementations are allowed, but not required, to recog- hcase clausei+) nize that some non-tail calls, such as the call to h above, can be (case hexpressioni evaluated as though they were tail calls. In the example above, hcase clausei* the let expression could be compiled as a tail call to h. (The (else htail sequencei)) possibility of h returning an unexpected number of values can be ignored, because in that case the effect of the let is explicitly (and hexpressioni* htail expressioni) unspecified and implementation-dependent.) (or hexpressioni* htail expressioni) 4. Expressions (let (hbinding speci*) htail bodyi) primitive derived (let hvariablei (hbinding speci*) htail bodyi) Expression types are categorized as or . (let* (hbinding speci*) htail bodyi) Primitive expression types include variables and procedure (letrec (hbinding speci*) htail bodyi) calls. Derived expression types are not semantically prim- itive, but can instead be defined as macros. With the ex- (let-syntax (hsyntax speci*) htail bodyi) ception of quasiquote, whose macro definition is complex, (letrec-syntax (hsyntax speci*) htail bodyi) the derived expressions are classified as library features. Suitable definitions are given in section 7.3. (begin htail sequencei) 4.1. Primitive expression types (do (hiteration speci*) (htesti htail sequencei) 4.1.1. Variable references hexpressioni*)

where hvariablei syntax An expression consisting of a variable (section 3.1) is a hcond clausei −→ (htesti htail sequencei) variable reference. The value of the variable reference is hcase clausei −→ ((hdatumi*) htail sequencei) the value stored in the location to which the variable is bound. It is an error to reference an unbound variable. htail bodyi −→ hdefinitioni* htail sequencei (define x 28) htail sequencei −→ hexpressioni* htail expressioni x =⇒ 28

• If a cond expression is in a tail context, and has a 4.1.2. Literal expressions clause of the form (hexpression1i => hexpression2i) then the (implied) call to the procedure that results (quote hdatumi) syntax from the evaluation of hexpression2i is in a tail context. ’hdatumi syntax hexpression2i itself is not in a tail context. hconstanti syntax (quote hdatumi) evaluates to hdatumi. hDatumi may be Certain built-in procedures are also required to perform any external representation of a Scheme object (see sec- tail calls. The first argument passed to apply and to tion 3.3). This notation is used to include literal constants call-with-current-continuation, and the second argu- in Scheme code. ment passed to call-with-values, must be called via a (quote a) =⇒ a tail call. Similarly, eval must evaluate its argument as if (quote #(a b c)) =⇒ #(a b c) it were in tail position within the eval procedure. (quote (+ 1 2)) =⇒ (+ 1 2) 4. Expressions 9

(quote hdatumi) may be abbreviated as ’hdatumi. The Note: In many dialects of Lisp, the empty combination, (), two notations are equivalent in all respects. is a legitimate expression. In Scheme, combinations must have at least one subexpression, so () is not a syntactically valid ’a =⇒ a expression. ’#(a b c) =⇒ #(a b c) ’() =⇒ () ’(+ 1 2) =⇒ (+ 1 2) 4.1.4. Procedures ’(quote a) =⇒ (quote a) ’’a =⇒ (quote a) (lambda hformalsi hbodyi) syntax Numerical constants, string constants, character constants, Syntax: hFormalsi should be a formal arguments list as and boolean constants evaluate “to themselves”; they need described below, and hbodyi should be a sequence of one not be quoted. or more expressions. Semantics: A lambda expression evaluates to a procedure. ’"abc" =⇒ "abc" The environment in effect when the lambda expression was "abc" =⇒ "abc" ’145932 =⇒ 145932 evaluated is remembered as part of the procedure. When 145932 =⇒ 145932 the procedure is later called with some actual arguments, ’#t =⇒ #t the environment in which the lambda expression was evalu- #t =⇒ #t ated will be extended by binding the variables in the formal argument list to fresh locations, the corresponding actual As noted in section 3.4, it is an error to alter a constant argument values will be stored in those locations, and the (i.e. the value of a literal expression) using a mutation pro- expressions in the body of the lambda expression will be cedure like set-car! or string-set!. evaluated sequentially in the extended environment. The result(s) of the last expression in the body will be returned 4.1.3. Procedure calls as the result(s) of the procedure call. (lambda (x) (+ x x)) =⇒ a procedure (hoperatori hoperand1i . . . ) syntax ((lambda (x) (+ x x)) 4) =⇒ 8

A procedure call is written by simply enclosing in paren- (define reverse-subtract theses expressions for the procedure to be called and the (lambda (x y) (- y x))) arguments to be passed to it. The operator and operand (reverse-subtract 7 10) =⇒ 3 expressions are evaluated (in an unspecified order) and the resulting procedure is passed the resulting arguments. (define add4 (+ 3 4) =⇒ 7 (let ((x 4)) ((if #f + *) 3 4) =⇒ 12 (lambda (y) (+ x y)))) (add4 6) =⇒ 10 A number of procedures are available as the values of vari- ables in the initial environment; for example, the addition hFormalsi should have one of the following forms: and multiplication procedures in the above examples are • (hvariable i . . . ): The procedure takes a fixed num- the values of the variables + and *. New procedures are cre- 1 ated by evaluating lambda expressions (see section 4.1.4). ber of arguments; when the procedure is called, the arguments will be stored in the bindings of the corre- Procedure calls may return any number of values (see sponding variables. values in section 6.4). With the exception of values the procedures available in the initial environment return one • hvariablei: The procedure takes any number of ar- value or, for procedures such as apply, pass on the values guments; when the procedure is called, the sequence returned by a call to one of their arguments. of actual arguments is converted into a newly allo- cated list, and the list is stored in the binding of the Procedure calls are also called combinations. hvariablei. Note: In contrast to other dialects of Lisp, the order of • (hvariable1i . . . hvariableni . hvariablen+1i): If a evaluation is unspecified, and the operator expression and the space-delimited period precedes the last variable, then operand expressions are always evaluated with the same evalu- the procedure takes n or more arguments, where n ation rules. is the number of formal arguments before the period Note: Although the order of evaluation is otherwise unspeci- (there must be at least one). The value stored in the fied, the effect of any concurrent evaluation of the operator and binding of the last variable will be a newly allocated operand expressions is constrained to be consistent with some list of the actual arguments left over after all the other sequential order of evaluation. The order of evaluation may be actual arguments have been matched up against the chosen differently for each procedure call. other formal arguments. 10 Revised5 Scheme

It is an error for a hvariablei to appear more than once in 4.2.1. Conditionals hformalsi. (cond hclause i hclause i . . . ) library syntax ((lambda x x) 3 4 5 6) =⇒ (3 4 5 6) 1 2 ((lambda (x y . z) z) Syntax: Each hclausei should be of the form 3 4 5 6) =⇒ (5 6) (htesti hexpression1i . . . ) Each procedure created as the result of evaluating a lambda where htesti is any expression. Alternatively, a hclausei expression is (conceptually) tagged with a storage location, may be of the form in order to make eqv? and eq? work on procedures (see (htesti => hexpressioni) section 6.1). The last hclausei may be an “else clause,” which has the form

4.1.5. Conditionals (else hexpression1i hexpression2i . . . ). Semantics: A cond expression is evaluated by evaluating (if htesti hconsequenti halternatei) syntax the htesti expressions of successive hclauseis in order until (if htesti hconsequenti) syntax one of them evaluates to a true value (see section 6.3.1). Syntax: hTesti, hconsequenti, and halternatei may be arbi- When a htesti evaluates to a true value, then the remain- trary expressions. ing hexpressionis in its hclausei are evaluated in order, and the result(s) of the last hexpressioni in the hclausei Semantics: An if expression is evaluated as follows: first, is(are) returned as the result(s) of the entire cond expres- htesti is evaluated. If it yields a true value (see sec- sion. If the selected hclausei contains only the htesti and no tion 6.3.1), then hconsequenti is evaluated and its value(s) hexpressionis, then the value of the htesti is returned as the is(are) returned. Otherwise halternatei is evaluated and its result. If the selected hclausei uses the => alternate form, value(s) is(are) returned. If htesti yields a false value and then the hexpressioni is evaluated. Its value must be a pro- no halternatei is specified, then the result of the expression cedure that accepts one argument; this procedure is then is unspecified. called on the value of the htesti and the value(s) returned (if (> 3 2) ’yes ’no) =⇒ yes by this procedure is(are) returned by the cond expression. (if (> 2 3) ’yes ’no) =⇒ no If all htestis evaluate to false values, and there is no else (if (> 3 2) clause, then the result of the conditional expression is un- (- 3 2) specified; if there is an else clause, then its hexpressionis are (+ 3 2)) =⇒ 1 evaluated, and the value(s) of the last one is(are) returned. (cond ((> 3 2) ’greater) 4.1.6. Assignments ((< 3 2) ’less)) =⇒ greater (cond ((> 3 3) ’greater) ((< 3 3) ’less) (set! hvariablei hexpressioni) syntax (else ’equal)) =⇒ equal hExpressioni is evaluated, and the resulting value is stored (cond ((assv ’b ’((a 1) (b 2))) => cadr) (else #f)) 2 in the location to which hvariablei is bound. hVariablei =⇒ must be bound either in some region enclosing the set! expression or at top level. The result of the set! expression (case hkeyi hclause1i hclause2i . . . ) library syntax is unspecified. Syntax: hKeyi may be any expression. Each hclausei (define x 2) should have the form (+ x 1) =⇒ 3 ((hdatum1i . . . ) hexpression1i hexpression2i . . . ), (set! x 4) =⇒ unspecified (+ x 1) =⇒ 5 where each hdatumi is an external representation of some object. All the hdatumis must be distinct. The last hclausei may be an “else clause,” which has the form

4.2. Derived expression types (else hexpression1i hexpression2i . . . ). Semantics: A case expression is evaluated as follows. The constructs in this section are hygienic, as discussed hKeyi is evaluated and its result is compared against each in section 4.3. For reference purposes, section 7.3 gives hdatumi. If the result of evaluating hkeyi is equivalent macro definitions that will convert most of the constructs (in the sense of eqv?; see section 6.1) to a hdatumi, then described in this section into the primitive constructs de- the expressions in the corresponding hclausei are evaluated scribed in the previous section. from left to right and the result(s) of the last expression in 4. Expressions 11 the hclausei is(are) returned as the result(s) of the case ex- initial values are being computed, thus allowing mutually pression. If the result of evaluating hkeyi is different from recursive definitions. every hdatumi, then if there is an else clause its expres- sions are evaluated and the result(s) of the last is(are) the (let hbindingsi hbodyi) library syntax result(s) of the case expression; otherwise the result of the case expression is unspecified. Syntax: hBindingsi should have the form

((hvariable1i hinit1i) . . . ), (case (* 2 3) ((2 3 5 7) ’prime) where each hiniti is an expression, and hbodyi should be a ((1 4 6 8 9) ’composite)) =⇒ composite sequence of one or more expressions. It is an error for a (case (car ’(c d)) hvariablei to appear more than once in the list of variables ((a) ’a) being bound. ((b) ’b)) =⇒ unspecified (case (car ’(c d)) Semantics: The hinitis are evaluated in the current envi- ((a e i o u) ’vowel) ronment (in some unspecified order), the hvariableis are ((w y) ’semivowel) bound to fresh locations holding the results, the hbodyi is (else ’consonant)) =⇒ consonant evaluated in the extended environment, and the value(s) of the last expression of hbodyi is(are) returned. Each bind- ing of a hvariablei has hbodyi as its region. (and htest1i . . . ) library syntax (let ((x 2) (y 3)) The htesti expressions are evaluated from left to right, and (* x y)) =⇒ 6 the value of the first expression that evaluates to a false value (see section 6.3.1) is returned. Any remaining ex- (let ((x 2) (y 3)) pressions are not evaluated. If all the expressions evaluate (let ((x 7) to true values, the value of the last expression is returned. (z (+ x y))) If there are no expressions then #t is returned. (* z x))) =⇒ 35

(and (= 2 2) (> 2 1)) =⇒ #t See also named let, section 4.2.4. (and (= 2 2) (< 2 1)) =⇒ #f (and 1 2 ’c ’(f g)) (f g) =⇒ (let* hbindingsi hbodyi) library syntax (and) =⇒ #t Syntax: hBindingsi should have the form

((hvariable1i hinit1i) . . . ), (or htest1i . . . ) library syntax and hbodyi should be a sequence of one or more expres- The htesti expressions are evaluated from left to right, and sions. the value of the first expression that evaluates to a true value (see section 6.3.1) is returned. Any remaining ex- Semantics: Let* is similar to let, but the bindings are pressions are not evaluated. If all expressions evaluate to performed sequentially from left to right, and the region of false values, the value of the last expression is returned. If a binding indicated by (hvariablei hiniti) is that part of there are no expressions then #f is returned. the let* expression to the right of the binding. Thus the second binding is done in an environment in which the first (or (= 2 2) (> 2 1)) =⇒ #t binding is visible, and so on. (or (= 2 2) (< 2 1)) =⇒ #t (or #f #f #f) =⇒ #f (let ((x 2) (y 3)) (or (memq ’b ’(a b c)) (let* ((x 7) (/ 3 0)) =⇒ (b c) (z (+ x y))) (* z x))) =⇒ 70

4.2.2. Binding constructs (letrec hbindingsi hbodyi) library syntax The three binding constructs let, let*, and letrec give Syntax: hBindingsi should have the form

Scheme a block structure, like Algol 60. The syntax of the ((hvariable1i hinit1i) . . . ), three constructs is identical, but they differ in the regions they establish for their variable bindings. In a let ex- and hbodyi should be a sequence of one or more expres- pression, the initial values are computed before any of the sions. It is an error for a hvariablei to appear more than variables become bound; in a let* expression, the bind- once in the list of variables being bound. ings and evaluations are performed sequentially; while in a Semantics: The hvariableis are bound to fresh locations letrec expression, all the bindings are in effect while their holding undefined values, the hinitis are evaluated in the 12 Revised5 Scheme resulting environment (in some unspecified order), each termination condition is met, the loop exits after evaluating hvariablei is assigned to the result of the corresponding the hexpressionis. hiniti, the hbodyi is evaluated in the resulting environment, Do expressions are evaluated as follows: The hiniti ex- and the value(s) of the last expression in hbodyi is(are) re- pressions are evaluated (in some unspecified order), the turned. Each binding of a hvariablei has the entire letrec hvariableis are bound to fresh locations, the results of expression as its region, making it possible to define mutu- the hiniti expressions are stored in the bindings of the ally recursive procedures. hvariableis, and then the iteration phase begins. (letrec ((even? Each iteration begins by evaluating htesti; if the result is (lambda (n) false (see section 6.3.1), then the hcommandi expressions (if (zero? n) are evaluated in order for effect, the hstepi expressions #t are evaluated in some unspecified order, the hvariableis (odd? (- n 1))))) (odd? are bound to fresh locations, the results of the hstepis are (lambda (n) stored in the bindings of the hvariableis, and the next iter- (if (zero? n) ation begins. #f If htesti evaluates to a true value, then the hexpressionis (even? (- n 1)))))) are evaluated from left to right and the value(s) of the (even? 88)) last hexpressioni is(are) returned. If no hexpressionis are =⇒ #t present, then the value of the do expression is unspecified. One restriction on letrec is very important: it must be The region of the binding of a hvariablei consists of the possible to evaluate each hiniti without assigning or refer- entire do expression except for the hinitis. It is an error ring to the value of any hvariablei. If this restriction is for a hvariablei to appear more than once in the list of do violated, then it is an error. The restriction is necessary variables. because Scheme passes arguments by value rather than by name. In the most common uses of letrec, all the hinitis A hstepi may be omitted, in which case the effect is the are lambda expressions and the restriction is satisfied au- same as if (hvariablei hiniti hvariablei) had been written tomatically. instead of (hvariablei hiniti). (do ((vec (make-vector 5)) (i 0 (+ i 1))) 4.2.3. Sequencing ((= i 5) vec) (vector-set! vec i i)) =⇒ #(0 1 2 3 4) (begin hexpression1i hexpression2i . . . ) library syntax (let ((x ’(1 3 5 7 9))) The hexpressionis are evaluated sequentially from left to (do ((x x (cdr x)) right, and the value(s) of the last hexpressioni is(are) re- (sum 0 (+ sum (car x)))) turned. This expression type is used to sequence side ef- ((null? x) sum))) =⇒ 25 fects such as input and output.

(define x 0) (let hvariablei hbindingsi hbodyi) library syntax

(begin (set! x 5) “Named let” is a variant on the syntax of let which pro- (+ x 1)) =⇒ 6 vides a more general looping construct than do and may also be used to express recursions. It has the same syn- (begin (display "4 plus 1 equals ") tax and semantics as ordinary let except that hvariablei (display (+ 4 1))) =⇒ unspecified is bound within hbodyi to a procedure whose formal argu- and prints 4 plus 1 equals 5 ments are the bound variables and whose body is hbodyi. Thus the execution of hbodyi may be repeated by invoking the procedure named by hvariablei. 4.2.4. Iteration (let loop ((numbers ’(3 -2 1 6 -5)) (do ((hvariable1i hinit1i hstep1i) library syntax (nonneg ’()) . . . ) (neg ’())) (htesti hexpressioni . . . ) (cond ((null? numbers) (list nonneg neg)) hcommandi . . . ) ((>= (car numbers) 0) (loop (cdr numbers) Do is an iteration construct. It specifies a set of variables (cons (car numbers) nonneg) to be bound, how they are to be initialized at the start, neg)) and how they are to be updated on each iteration. When a ((< (car numbers) 0) 4. Expressions 13

(loop (cdr numbers) `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) nonneg =⇒ (a `(b ,(+ 1 2) ,(foo 4 d) e) f) (cons (car numbers) neg))))) (let ((name1 ’x) =⇒ ((6 1 3) (-5 -2)) (name2 ’y)) `(a `(b ,,name1 ,’,name2 d) e)) =⇒ (a `(b ,x ,’y d) e) 4.2.5. Delayed evaluation The two notations `hqq templatei and (quasiquote hqq templatei) are identical in all respects. ,hexpressioni (delay hexpressioni) library syntax is identical to (unquote hexpressioni), and ,@hexpressioni The delay construct is used together with the proce- is identical to (unquote-splicing hexpressioni). The ex- dure force to implement lazy evaluation or call by need. ternal syntax generated by write for two-element lists (delay hexpressioni) returns an object called a promise whose car is one of these symbols may vary between im- which at some point in the future may be asked (by the plementations. force procedure) to evaluate hexpressioni, and deliver the (quasiquote (list (unquote (+ 1 2)) 4)) resulting value. The effect of hexpressioni returning multi- =⇒ (list 3 4) ple values is unspecified. ’(quasiquote (list (unquote (+ 1 2)) 4)) See the description of force (section 6.4) for a more com- =⇒ `(list ,(+ 1 2) 4) plete description of delay. i.e., (quasiquote (list (unquote (+ 1 2)) 4)) Unpredictable behavior can result if any of the symbols 4.2.6. Quasiquotation quasiquote, unquote, or unquote-splicing appear in po- sitions within a hqq templatei otherwise than as described above. (quasiquote hqq templatei) syntax `hqq templatei syntax “Backquote” or “quasiquote” expressions are useful for 4.3. Macros constructing a list or vector structure when most but not all of the desired structure is known in advance. If no Scheme programs can define and use new derived expres- commas appear within the hqq templatei, the result of sion types, called macros. Program-defined expression evaluating `hqq templatei is equivalent to the result of types have the syntax evaluating ’hqq templatei. If a comma appears within (hkeywordi hdatumi ...) the hqq templatei, however, the expression following the where hkeywordi is an identifier that uniquely determines comma is evaluated (“unquoted”) and its result is inserted the expression type. This identifier is called the syntactic into the structure instead of the comma and the expres- keyword, or simply keyword, of the macro. The number of sion. If a comma appears followed immediately by an at- the hdatumis, and their syntax, depends on the expression sign (@), then the following expression must evaluate to type. a list; the opening and closing parentheses of the list are then “stripped away” and the elements of the list are in- Each instance of a macro is called a use of the macro. The serted in place of the comma at-sign expression sequence. set of rules that specifies how a use of a macro is transcribed A comma at-sign should only appear within a list or vector into a more primitive expression is called the transformer hqq templatei. of the macro.

`(list ,(+ 1 2) 4) =⇒ (list 3 4) The macro definition facility consists of two parts: (let ((name ’a)) `(list ,name ’,name)) =⇒ (list a (quote a)) • A set of expressions used to establish that certain iden- `(a ,(+ 1 2) ,@(map abs ’(4 -5 6)) b) tifiers are macro keywords, associate them with macro =⇒ (a 3 4 5 6 b) transformers, and control the scope within which a `(( foo ,(- 10 3)) ,@(cdr ’(c)) . ,(car ’(cons))) macro is defined, and =⇒ ((foo 7) . cons) `#(10 5 ,(sqrt 4) ,@(map sqrt ’(16 9)) 8) • a pattern language for specifying macro transformers. =⇒ #(10 5 2 4 3 8) Quasiquote forms may be nested. Substitutions are made The syntactic keyword of a macro may shadow variable only for unquoted components appearing at the same nest- bindings, and local variable bindings may shadow keyword ing level as the outermost backquote. The nesting level in- bindings. All macros defined using the pattern language creases by one inside each successive quasiquotation, and are “hygienic” and “referentially transparent” and thus decreases by one inside each unquotation. preserve Scheme’s lexical scoping [14, 15, 2, 7, 9]: 14 Revised5 Scheme

• If a macro transformer inserts a binding for an identi- of the letrec-syntax expression with macros whose key- fier (variable or keyword), the identifier will in effect be words are the hkeywordis, bound to the specified trans- renamed throughout its scope to avoid conflicts with formers. Each binding of a hkeywordi has the hbindingsi other identifiers. Note that a define at top level may as well as the hbodyi within its region, so the transformers or may not introduce a binding; see section 5.2. can transcribe expressions into uses of the macros intro- duced by the letrec-syntax expression. • If a macro transformer inserts a free reference to an identifier, the reference refers to the binding that was (letrec-syntax visible where the transformer was specified, regardless ((my-or (syntax-rules () ((my-or) #f) of any local bindings that may surround the use of the ((my-or e) e) macro. ((my-or e1 e2 ...) (let ((temp e1)) (if temp 4.3.1. Binding constructs for syntactic keywords temp (my-or e2 ...))))))) Let-syntax and letrec-syntax are analogous to let and (let ((x #f) letrec, but they bind syntactic keywords to macro trans- (y 7) formers instead of binding variables to locations that con- (temp 8) tain values. Syntactic keywords may also be bound at top (let odd?) level; see section 5.3. (if even?)) (my-or x (let temp) (let-syntax hbindingsi hbodyi) syntax (if y) Syntax: hBindingsi should have the form y))) =⇒ 7 ((hkeywordi htransformer speci) . . . ) 4.3.2. Pattern language Each hkeywordi is an identifier, each htransformer speci is an instance of syntax-rules, and hbodyi should be a A htransformer speci has the following form: sequence of one or more expressions. It is an error for a hkeywordi to appear more than once in the list of keywords being bound. (syntax-rules hliteralsi hsyntax rulei . . . ) Syntax: hLiteralsi is a list of identifiers and each Semantics: The hbodyi is expanded in the syntactic envi- hsyntax rulei should be of the form ronment obtained by extending the syntactic environment of the let-syntax expression with macros whose keywords (hpatterni htemplatei) are the hkeywordis, bound to the specified transformers. The hpatterni in a hsyntax rulei is a list hpatterni that Each binding of a hkeywordi has hbodyi as its region. begins with the keyword for the macro. (let-syntax ((when (syntax-rules () A hpatterni is either an identifier, a constant, or one of the ((when test stmt1 stmt2 ...) following (if test (hpatterni ...) (begin stmt1 (hpatterni hpatterni ... . hpatterni) stmt2 ...)))))) (hpatterni ... hpatterni hellipsisi) (let ((if #t)) #(hpatterni ...) (when if (set! if ’now)) #(hpatterni ... hpatterni hellipsisi) if)) =⇒ now and a template is either an identifier, a constant, or one of (let ((x ’outer)) the following (let-syntax ((m (syntax-rules () ((m) x)))) (helementi ...) (let ((x ’inner)) (helementi helementi ... . htemplatei) (m)))) =⇒ outer #(helementi ...) where an helementi is a htemplatei optionally followed by an hellipsisi and an hellipsisi is the identifier “...” (which (letrec-syntax hbindingsi hbodyi) syntax cannot be used as an identifier in either a template or a Syntax: Same as for let-syntax. pattern). Semantics: The hbodyi is expanded in the syntactic envi- Semantics: An instance of syntax-rules produces a new ronment obtained by extending the syntactic environment macro transformer by specifying a sequence of hygienic 4. Expressions 15 rewrite rules. A use of a macro whose keyword is associated • P is a vector of the form #(P1 . . . Pn) and F is a with a transformer specified by syntax-rules is matched vector of n forms that match P1 through Pn; or against the patterns contained in the hsyntax ruleis, be- ginning with the leftmost hsyntax rulei. When a match is • P is of the form #(P1 . . . Pn Pn+1 hellipsisi) where found, the macro use is transcribed hygienically according hellipsisi is the identifier ... and F is a vector of n or to the template. more forms the first n of which match P1 through Pn, respectively, and each remaining element of F matches An identifier that appears in the pattern of a hsyntax rulei Pn+1; or is a pattern variable, unless it is the keyword that begins the pattern, is listed in hliteralsi, or is the identifier “...”. • P is a datum and F is equal to P in the sense of the Pattern variables match arbitrary input elements and are equal? procedure. used to refer to elements of the input in the template. It is an error for the same pattern variable to appear more It is an error to use a macro keyword, within the scope of than once in a hpatterni. its binding, in an expression that does not match any of The keyword at the beginning of the pattern in a the patterns. hsyntax rulei is not involved in the matching and is not When a macro use is transcribed according to the template considered a pattern variable or literal identifier. of the matching hsyntax rulei, pattern variables that occur Rationale: The scope of the keyword is determined by the in the template are replaced by the subforms they match expression or syntax definition that binds it to the associated in the input. Pattern variables that occur in subpatterns macro transformer. If the keyword were a pattern variable or followed by one or more instances of the identifier ... are literal identifier, then the template that follows the pattern allowed only in subtemplates that are followed by as many would be within its scope regardless of whether the keyword instances of .... They are replaced in the output by all were bound by let-syntax or by letrec-syntax. of the subforms they match in the input, distributed as indicated. It is an error if the output cannot be built up Identifiers that appear in hliteralsi are interpreted as literal as specified. identifiers to be matched against corresponding subforms of the input. A subform in the input matches a literal Identifiers that appear in the template but are not pattern identifier if and only if it is an identifier and either both its variables or the identifier ... are inserted into the output occurrence in the macro expression and its occurrence in as literal identifiers. If a literal identifier is inserted as a the macro definition have the same lexical binding, or the free identifier then it refers to the binding of that identifier two identifiers are equal and both have no lexical binding. within whose scope the instance of syntax-rules appears. If a literal identifier is inserted as a bound identifier then A subpattern followed by ... can match zero or more el- it is in effect renamed to prevent inadvertent captures of ements of the input. It is an error for ... to appear in free identifiers. hliteralsi. Within a pattern the identifier ... must follow the last element of a nonempty sequence of subpatterns. As an example, if let and cond are defined as in section 7.3 then they are hygienic (as required) and the following is not More formally, an input form F matches a pattern P if and an error. only if: (let ((=> #f)) (cond (#t => ’ok))) =⇒ ok • P is a non-literal identifier; or The macro transformer for cond recognizes => as a local • P is a literal identifier and F is an identifier with the variable, and hence an expression, and not as the top-level same binding; or identifier =>, which the macro transformer treats as a syn-

• P is a list (P1 . . . Pn) and F is a list of n forms that tactic keyword. Thus the example expands into match P1 through Pn, respectively; or (let ((=> #f)) (if #t (begin => ’ok))) • P is an improper list (P1 P2 . . . Pn . Pn+1) and F is a list or improper list of n or more forms that instead of match P1 through Pn, respectively, and whose nth (let ((=> #f)) “cdr” matches P ; or n+1 (let ((temp #t)) (if temp (’ok temp)))) • P is of the form (P1 . . . Pn Pn+1 hellipsisi) where hellipsisi is the identifier ... and F is a proper list which would result in an invalid procedure call. of at least n forms, the first n of which match P1 through Pn, respectively, and each remaining element of F matches Pn+1; or 16 Revised5 Scheme

5. Program structure 5.2.1. Top level definitions 5.1. Programs At the top level of a program, a definition (define hvariablei hexpressioni) A Scheme program consists of a sequence of expressions, has essentially the same effect as the assignment expres- definitions, and syntax definitions. Expressions are de- sion scribed in chapter 4; definitions and syntax definitions are (set! hvariablei hexpressioni) the subject of the rest of the present chapter. if hvariablei is bound. If hvariablei is not bound, however, Programs are typically stored in files or entered inter- then the definition will bind hvariablei to a new location actively to a running Scheme system, although other before performing the assignment, whereas it would be an paradigms are possible; questions of user interface lie out- error to perform a set! on an unbound variable. side the scope of this report. (Indeed, Scheme would still be useful as a notation for expressing computational methods (define add3 even in the absence of a mechanical implementation.) (lambda (x) (+ x 3))) (add3 3) =⇒ 6 Definitions and syntax definitions occurring at the top level (define first car) of a program can be interpreted declaratively. They cause (first ’(1 2)) =⇒ 1 bindings to be created in the top level environment or mod- Some implementations of Scheme use an initial environ- ify the value of existing top-level bindings. Expressions ment in which all possible variables are bound to locations, occurring at the top level of a program are interpreted im- most of which contain undefined values. Top level defini- peratively; they are executed in order when the program tions in such an implementation are truly equivalent to is invoked or loaded, and typically perform some kind of assignments. initialization. At the top level of a program (begin hform i . . . ) is 1 5.2.2. Internal definitions equivalent to the sequence of expressions, definitions, and syntax definitions that form the body of the begin. Definitions may occur at the beginning of a hbodyi (that is, the body of a lambda, let, let*, letrec, let-syntax, or letrec-syntax expression or that of a definition of an 5.2. Definitions appropriate form). Such definitions are known as internal definitions as opposed to the top level definitions described Definitions are valid in some, but not all, contexts where above. The variable defined by an internal definition is expressions are allowed. They are valid only at the top local to the hbodyi. That is, hvariablei is bound rather level of a hprogrami and at the beginning of a hbodyi. than assigned, and the region of the binding is the entire hbodyi. For example, A definition should have one of the following forms: (let ((x 5)) (define foo (lambda (y) (bar x y))) • (define hvariablei hexpressioni) (define bar (lambda (a b) (+ (* a b) a))) (foo (+ x 3))) =⇒ 45 • (define (hvariablei hformalsi) hbodyi) A hbodyi containing internal definitions can always be con- hFormalsi should be either a sequence of zero or more verted into a completely equivalent letrec expression. For variables, or a sequence of one or more variables fol- example, the let expression in the above example is equiv- lowed by a space-delimited period and another vari- able (as in a lambda expression). This form is equiv- alent to alent to (let ((x 5)) (letrec ((foo (lambda (y) (bar x y))) (define hvariablei (bar (lambda (a b) (+ (* a b) a)))) (lambda (hformalsi) hbodyi)). (foo (+ x 3)))) Just as for the equivalent letrec expression, it must be • (define (hvariablei . hformali) hbodyi) possible to evaluate each hexpressioni of every internal def- hFormali should be a single variable. This form is inition in a hbodyi without assigning or referring to the equivalent to value of any hvariablei being defined. Wherever an internal definition may occur (begin (define hvariablei hdefinition1i . . . ) is equivalent to the sequence of defini- (lambda hformali hbodyi)). tions that form the body of the begin. 6. Standard procedures 17

5.3. Syntax definitions top-level binding that has not been introduced by a defini- tion has an unspecified effect on the behavior of the built-in Syntax definitions are valid only at the top level of a procedures. hprogrami. They have the following form: (define-syntax hkeywordi htransformer speci) 6.1. Equivalence predicates hKeywordi is an identifier, and the htransformer speci should be an instance of syntax-rules. The top-level syn- A predicate is a procedure that always returns a boolean tactic environment is extended by binding the hkeywordi value (#t or #f). An equivalence predicate is the compu- to the specified transformer. tational analogue of a mathematical equivalence relation (it is symmetric, reflexive, and transitive). Of the equiva- There is no define-syntax analogue of internal defini- lence predicates described in this section, eq? is the finest tions. or most discriminating, and equal? is the coarsest. Eqv? Although macros may expand into definitions and syntax is slightly less discriminating than eq?. definitions in any context that permits them, it is an error for a definition or syntax definition to shadow a syntactic (eqv? obj1 obj2) procedure keyword whose meaning is needed to determine whether some form in the group of forms that contains the shad- The eqv? procedure defines a useful equivalence relation owing definition is in fact a definition, or, for internal def- on objects. Briefly, it returns #t if obj1 and obj2 should initions, is needed to determine the boundary between the normally be regarded as the same object. This relation is group and the expressions that follow the group. For ex- left slightly open to interpretation, but the following par- ample, the following are errors: tial specification of eqv? holds for all implementations of Scheme. (define define 3) The eqv? procedure returns #t if: (begin (define begin list)) • obj1 and obj2 are both #t or both #f. (let-syntax ((foo (syntax-rules () • obj1 and obj2 are both symbols and ((foo (proc args ...) body ...) (define proc (string=? (symbol->string obj1) (lambda (args ...) (symbol->string obj2)) body ...)))))) =⇒ #t (let ((x 3))

(foo (plus x y) (+ x y)) Note: This assumes that neither obj1 nor obj2 is an “un- (define foo x) interned symbol” as alluded to in section 6.3.3. This re- (plus foo x))) port does not presume to specify the behavior of eqv? on implementation-dependent extensions.

• obj1 and obj2 are both numbers, are numerically equal 6. Standard procedures (see =, section 6.2), and are either both exact or both inexact. This chapter describes Scheme’s built-in procedures. The initial (or “top level”) Scheme environment starts out with • obj1 and obj2 are both characters and are the same a number of variables bound to locations containing useful character according to the char=? procedure (sec- values, most of which are primitive procedures that ma- tion 6.3.4). nipulate data. For example, the variable abs is bound to (a location initially containing) a procedure of one argu- • both obj1 and obj2 are the empty list. ment that computes the absolute value of a number, and • obj and obj are pairs, vectors, or strings that denote the variable + is bound to a procedure that computes sums. 1 2 the same locations in the store (section 3.4). Built-in procedures that can easily be written in terms of other built-in procedures are identified as “library proce- • obj1 and obj2 are procedures whose location tags are dures”. equal (section 4.1.4). A program may use a top-level definition to bind any vari- able. It may subsequently alter any such binding by an The eqv? procedure returns #f if: assignment (see 4.1.6). These operations do not modify the behavior of Scheme’s built-in procedures. Altering any • obj1 and obj2 are of different types (section 3.2). 18 Revised5 Scheme

• one of obj1 and obj2 is #t but the other is #f. (define gen-counter (lambda () • obj1 and obj2 are symbols but (let ((n 0)) (lambda () (set! n (+ n 1)) n)))) (let ((g (gen-counter))) (string=? (symbol->string obj1) (eqv? g g)) =⇒ #t (symbol->string obj2)) (eqv? (gen-counter) (gen-counter)) =⇒ #f =⇒ #f (define gen-loser (lambda () • one of obj1 and obj2 is an exact number but the other (let ((n 0)) is an inexact number. (lambda () (set! n (+ n 1)) 27)))) (let ((g (gen-loser))) • obj and obj are numbers for which the = procedure 1 2 (eqv? g g)) =⇒ #t returns #f. (eqv? (gen-loser) (gen-loser)) =⇒ unspecified • obj1 and obj2 are characters for which the char=? pro- cedure returns #f. (letrec ((f (lambda () (if (eqv? f g) ’both ’f))) (g (lambda () (if (eqv? f g) ’both ’g)))) • one of obj1 and obj2 is the empty list but the other is (eqv? f g)) not. =⇒ unspecified

• obj1 and obj2 are pairs, vectors, or strings that denote (letrec ((f (lambda () (if (eqv? f g) ’f ’both))) distinct locations. (g (lambda () (if (eqv? f g) ’g ’both)))) (eqv? f g)) =⇒ #f • obj1 and obj2 are procedures that would behave differ- ently (return different value(s) or have different side Since it is an error to modify constant objects (those re- effects) for some arguments. turned by literal expressions), implementations are per- mitted, though not required, to share structure between (eqv? ’a ’a) =⇒ #t constants where appropriate. Thus the value of eqv? on (eqv? ’a ’b) =⇒ #f constants is sometimes implementation-dependent. (eqv? 2 2) =⇒ #t (eqv? ’() ’()) =⇒ #t (eqv? ’(a) ’(a)) =⇒ unspecified (eqv? 100000000 100000000) =⇒ #t (eqv? "a" "a") =⇒ unspecified (eqv? (cons 1 2) (cons 1 2))=⇒ #f (eqv? ’(b) (cdr ’(a b))) =⇒ unspecified (eqv? (lambda () 1) (let ((x ’(a))) (lambda () 2)) =⇒ #f (eqv? x x)) =⇒ #t (eqv? #f ’nil) =⇒ #f (let ((p (lambda (x) x))) Rationale: The above definition of eqv? allows implementa- (eqv? p p)) =⇒ #t tions latitude in their treatment of procedures and literals: im- plementations are free either to detect or to fail to detect that The following examples illustrate cases in which the above two procedures or two literals are equivalent to each other, and rules do not fully specify the behavior of eqv?. All that can decide whether or not to merge representations of equivalent can be said about such cases is that the value returned by objects by using the same pointer or bit pattern to represent eqv? must be a boolean. both.

(eqv? "" "") =⇒ unspecified (eqv? ’#() ’#()) =⇒ unspecified (eq? obj1 obj2) procedure (eqv? (lambda (x) x) Eq? is similar to eqv? except that in some cases it is capable (lambda (x) x)) =⇒ unspecified of discerning distinctions finer than those detectable by (eqv? (lambda (x) x) eqv?. (lambda (y) y)) =⇒ unspecified Eq? and eqv? are guaranteed to have the same behavior on The next set of examples shows the use of eqv? with pro- symbols, booleans, the empty list, pairs, procedures, and cedures that have local state. Gen-counter must return a non-empty strings and vectors. Eq?’s behavior on numbers distinct procedure every time, since each procedure has its and characters is implementation-dependent, but it will al- own internal counter. Gen-loser, however, returns equiv- ways return either true or false, and will return true only alent procedures each time, since the local state does not when eqv? would also return true. Eq? may also behave affect the value or side effects of the procedures. differently from eqv? on empty vectors and empty strings. 6. Standard procedures 19

(eq? ’a ’a) =⇒ #t It is important to distinguish between the mathemati- (eq? ’(a) ’(a)) =⇒ unspecified cal numbers, the Scheme numbers that attempt to model (eq? (list ’a) (list ’a)) =⇒ #f them, the machine representations used to implement the (eq? "a" "a") =⇒ unspecified Scheme numbers, and notations used to write numbers. (eq? "" "") =⇒ unspecified This report uses the types number, complex, real, rational, (eq? ’() ’()) =⇒ #t and integer to refer to both mathematical numbers and (eq? 2 2) =⇒ unspecified Scheme numbers. Machine representations such as fixed (eq? #\A #\A) =⇒ unspecified (eq? car car) =⇒ #t point and floating point are referred to by names such as (let ((n (+ 2 3))) fixnum and flonum. (eq? n n)) =⇒ unspecified (let ((x ’(a))) 6.2.1. Numerical types (eq? x x)) =⇒ #t (let ((x ’#())) Mathematically, numbers may be arranged into a tower of (eq? x x)) #t =⇒ subtypes in which each level is a subset of the level above (let ((p (lambda (x) x))) it: (eq? p p)) =⇒ #t number Rationale: It will usually be possible to implement eq? much complex more efficiently than eqv?, for example, as a simple pointer com- real parison instead of as some more complicated operation. One rational reason is that it may not be possible to compute eqv? of two integer numbers in constant time, whereas eq? implemented as pointer comparison will always finish in constant time. Eq? may be used For example, 3 is an integer. Therefore 3 is also a rational, like eqv? in applications using procedures to implement objects a real, and a complex. The same is true of the Scheme with state since it obeys the same constraints as eqv?. numbers that model 3. For Scheme numbers, these types are defined by the predicates number?, complex?, real?, rational?, and integer?. (equal? obj1 obj2) library procedure There is no simple relationship between a number’s type Equal? recursively compares the contents of pairs, vectors, and its representation inside a computer. Although most and strings, applying eqv? on other objects such as num- implementations of Scheme will offer at least two different bers and symbols. A rule of thumb is that objects are representations of 3, these different representations denote generally equal? if they print the same. Equal? may fail the same integer. to terminate if its arguments are circular data structures. Scheme’s numerical operations treat numbers as abstract (equal? ’a ’a) =⇒ #t data, as independent of their representation as possible. (equal? ’(a) ’(a)) =⇒ #t (equal? ’(a (b) c) Although an implementation of Scheme may use fixnum, ’(a (b) c)) =⇒ #t flonum, and perhaps other representations for numbers, (equal? "abc" "abc") =⇒ #t this should not be apparent to a casual programmer writing (equal? 2 2) =⇒ #t simple programs. (equal? (make-vector 5 ’a) It is necessary, however, to distinguish between numbers (make-vector 5 ’a)) =⇒ #t that are represented exactly and those that may not be. (equal? (lambda (x) x) (lambda (y) y)) =⇒ unspecified For example, indexes into data structures must be known exactly, as must some polynomial coefficients in a symbolic algebra system. On the other hand, the results of measure- 6.2. Numbers ments are inherently inexact, and irrational numbers may be approximated by rational and therefore inexact approx- Numerical computation has traditionally been neglected imations. In order to catch uses of inexact numbers where by the Lisp community. Until Common Lisp there was exact numbers are required, Scheme explicitly distinguishes no carefully thought out strategy for organizing numerical exact from inexact numbers. This distinction is orthogonal computation, and with the exception of the MacLisp sys- to the dimension of type. tem [20] little effort was made to execute numerical code efficiently. This report recognizes the excellent work of the 6.2.2. Exactness Common Lisp committee and accepts many of their rec- ommendations. In some ways this report simplifies and Scheme numbers are either exact or inexact. A number is generalizes their proposals in a manner consistent with the exact if it was written as an exact constant or was derived purposes of Scheme. from exact numbers using only exact operations. A number 20 Revised5 Scheme is inexact if it was written as an inexact constant, if it must return an exact integer, and it is an error to use was derived using inexact ingredients, or if it was derived anything but an exact integer as an index. Furthermore using inexact operations. Thus inexactness is a contagious any integer constant within the index range, if expressed property of a number. by an exact integer syntax, will indeed be read as an exact If two implementations produce exact results for a com- integer, regardless of any implementation restrictions that putation that did not involve inexact intermediate results, may apply outside this range. Finally, the procedures listed the two ultimate results will be mathematically equivalent. below will always return an exact integer result provided all This is generally not true of computations involving inex- their arguments are exact integers and the mathematically act numbers since approximate methods such as floating expected result is representable as an exact integer within point arithmetic may be used, but it is the duty of each the implementation: implementation to make the result as close as practical to + - * the mathematically ideal result. quotient remainder modulo Rational operations such as + should always produce ex- max min abs act results when given exact arguments. If the operation numerator denominator gcd lcm floor ceiling is unable to produce an exact result, then it may either truncate round rationalize report the violation of an implementation restriction or it expt may silently coerce its result to an inexact value. See sec- tion 6.2.3. Implementations are encouraged, but not required, to sup- With the exception of inexact->exact, the operations de- port exact integers and exact rationals of practically unlim- scribed in this section must generally return inexact results ited size and precision, and to implement the above proce- when given any inexact arguments. An operation may, dures and the / procedure in such a way that they always however, return an exact result if it can prove that the return exact results when given exact arguments. If one of value of the result is unaffected by the inexactness of its these procedures is unable to deliver an exact result when arguments. For example, multiplication of any number by given exact arguments, then it may either report a vio- an exact zero may produce an exact zero result, even if the lation of an implementation restriction or it may silently other argument is inexact. coerce its result to an inexact number. Such a coercion may cause an error later.

6.2.3. Implementation restrictions An implementation may use floating point and other ap- proximate representation strategies for inexact numbers. Implementations of Scheme are not required to implement This report recommends, but does not require, that the the whole tower of subtypes given in section 6.2.1, but IEEE 32-bit and 64-bit floating point standards be followed they must implement a coherent subset consistent with by implementations that use flonum representations, and both the purposes of the implementation and the spirit that implementations using other representations should of the Scheme language. For example, an implementation match or exceed the precision achievable using these float- in which all numbers are real may still be quite useful. ing point standards [12]. Implementations may also support only a limited range of In particular, implementations that use flonum represen- numbers of any type, subject to the requirements of this tations must follow these rules: A flonum result must be section. The supported range for exact numbers of any represented with at least as much precision as is used to type may be different from the supported range for inex- express any of the inexact arguments to that operation. It act numbers of that type. For example, an implementation is desirable (but not required) for potentially inexact oper- that uses flonums to represent all its inexact real numbers ations such as sqrt, when applied to exact arguments, to may support a practically unbounded range of exact inte- produce exact answers whenever possible (for example the gers and rationals while limiting the range of inexact reals square root of an exact 4 ought to be an exact 2). If, how- (and therefore the range of inexact integers and rationals) ever, an exact number is operated upon so as to produce an to the dynamic range of the flonum format. Furthermore inexact result (as by sqrt), and if the result is represented the gaps between the representable inexact integers and ra- as a flonum, then the most precise flonum format available tionals are likely to be very large in such an implementation must be used; but if the result is represented in some other as the limits of this range are approached. way then the representation must have at least as much precision as the most precise flonum format available. An implementation of Scheme must support exact integers throughout the range of numbers that may be used for Although Scheme allows a variety of written notations for indexes of lists, vectors, and strings or that may result numbers, any particular implementation may support only from computing the length of a list, vector, or string. The some of them. For example, an implementation in which length, vector-length, and string-length procedures all numbers are real need not support the rectangular and 6. Standard procedures 21 polar notations for complex numbers. If an implementa- be represented without loss of accuracy; the inexact con- tion encounters an exact numerical constant that it cannot stants were chosen so that this is likely to be true in imple- represent as an exact number, then it may either report a mentations that use flonums to represent inexact numbers. violation of an implementation restriction or it may silently represent the constant by an inexact number. (number? obj ) procedure (complex? obj ) procedure 6.2.4. Syntax of numerical constants (real? obj ) procedure (rational? obj ) procedure The syntax of the written representations for numbers is (integer? obj ) procedure described formally in section 7.1.1. Note that case is not These numerical type predicates can be applied to any kind significant in numerical constants. of argument, including non-numbers. They return #t if the A number may be written in binary, octal, decimal, or hex- object is of the named type, and otherwise they return #f. adecimal by the use of a radix prefix. The radix prefixes In general, if a type predicate is true of a number then are #b (binary), #o (octal), #d (decimal), and #x (hexadec- all higher type predicates are also true of that number. imal). With no radix prefix, a number is assumed to be Consequently, if a type predicate is false of a number, then expressed in decimal. all lower type predicates are also false of that number. A numerical constant may be specified to be either exact or If z is an inexact complex number, then (real? z) is true inexact by a prefix. The prefixes are #e for exact, and #i if and only if (zero? (imag-part z)) is true. If x is an for inexact. An exactness prefix may appear before or after inexact real number, then (integer? x) is true if and only any radix prefix that is used. If the written representation if (= x (round x)). of a number has no exactness prefix, the constant may be (complex? 3+4i) =⇒ #t either inexact or exact. It is inexact if it contains a decimal (complex? 3) =⇒ #t point, an exponent, or a “#” character in the place of a (real? 3) =⇒ #t digit, otherwise it is exact. (real? -2.5+0.0i) =⇒ #t (real? #e1e10) =⇒ #t In systems with inexact numbers of varying precisions it (rational? 6/10) =⇒ #t may be useful to specify the precision of a constant. For (rational? 6/3) =⇒ #t this purpose, numerical constants may be written with an (integer? 3+0i) =⇒ #t exponent marker that indicates the desired precision of the (integer? 3.0) =⇒ #t inexact representation. The letters s, f, d, and l specify (integer? 8/4) =⇒ #t short single double long the use of , , , and precision, respec- Note: The behavior of these type predicates on inexact num- tively. (When fewer than four internal inexact represen- bers is unreliable, since any inaccuracy may affect the result. tations exist, the four size specifications are mapped onto those available. For example, an implementation with two Note: In many implementations the rational? procedure will internal representations may map short and single together be the same as real?, and the complex? procedure will be the and long and double together.) In addition, the exponent same as number?, but unusual implementations may be able marker e specifies the default precision for the implemen- to represent some irrational numbers exactly or may extend the tation. The default precision has at least as much precision number system to support some kind of non-complex numbers. as double, but implementations may wish to allow this de- fault to be set by the user. (exact? z) procedure (inexact? z) procedure 3.14159265358979F0 Round to single — 3.141593 These numerical predicates provide tests for the exactness 0.6L0 of a quantity. For any Scheme number, precisely one of Extend to long — .600000000000000 these predicates is true.

(= z z z . . . ) procedure 6.2.5. Numerical operations 1 2 3 (< x1 x2 x3 . . . ) procedure The reader is referred to section 1.3.3 for a summary of (> x1 x2 x3 . . . ) procedure the naming conventions used to specify restrictions on the (<= x1 x2 x3 . . . ) procedure types of arguments to numerical routines. The examples (>= x1 x2 x3 . . . ) procedure used in this section assume that any numerical constant These procedures return #t if their arguments are (respec- written using an exact notation is indeed represented as tively): equal, monotonically increasing, monotonically de- an exact number. Some examples also assume that certain creasing, monotonically nondecreasing, or monotonically numerical constants written using an inexact notation can nonincreasing. 22 Revised5 Scheme

These predicates are required to be transitive. (- 3 4) =⇒ -1 (- 3 4 5) =⇒ -6 Note: The traditional implementations of these predicates in (- 3) =⇒ -3 Lisp-like languages are not transitive. (/ 3 4 5) =⇒ 3/20 Note: While it is not an error to compare inexact numbers (/ 3) =⇒ 1/3 using these predicates, the results may be unreliable because a small inaccuracy may affect the result; this is especially true of = and zero?. When in doubt, consult a numerical analyst. (abs x) library procedure Abs returns the absolute value of its argument. (zero? z) library procedure (positive? x) library procedure (abs -7) =⇒ 7 (negative? x) library procedure (odd? n) library procedure (even? n) library procedure (quotient n1 n2) procedure These numerical predicates test a number for a particular (remainder n1 n2) procedure #t #f property, returning or . See note above. (modulo n1 n2) procedure These procedures implement number-theoretic (integer) di- (max x x . . . ) library procedure 1 2 vision. n2 should be non-zero. All three procedures return (min x1 x2 . . . ) library procedure integers. If n1/n2 is an integer: These procedures return the maximum or minimum of their (quotient n1 n2) =⇒ n1/n2 arguments. (remainder n1 n2) =⇒ 0 (max 3 4) =⇒ 4 ; exact (modulo n1 n2) =⇒ 0 (max 3.9 4) =⇒ 4.0 ; inexact Note: If any argument is inexact, then the result will also be If n1/n2 is not an integer: inexact (unless the procedure can prove that the inaccuracy is (quotient n1 n2) =⇒ nq not large enough to affect the result, which is possible only in (remainder n1 n2) =⇒ nr unusual implementations). If min or max is used to compare (modulo n1 n2) =⇒ nm numbers of mixed exactness, and the numerical value of the result cannot be represented as an inexact number without loss where n is n /n rounded towards zero, 0 < |n | < |n |, of accuracy, then the procedure may report a violation of an q 1 2 r 2 0 < |n | < |n |, n and n differ from n by a multiple of implementation restriction. m 2 r m 1 n2, nr has the same sign as n1, and nm has the same sign as n2. (+ z1 . . . ) procedure From this we can conclude that for integers n1 and n2 with (* z1 . . . ) procedure n2 not equal to 0, These procedures return the sum or product of their argu- ments. (= n1 (+ (* n2 (quotient n1 n2)) (remainder n1 n2))) (+ 3 4) =⇒ 7 =⇒ #t (+ 3) =⇒ 3 (+) =⇒ 0 provided all numbers involved in that computation are ex- (* 4) =⇒ 4 act. (*) =⇒ 1 (modulo 13 4) =⇒ 1 (remainder 13 4) =⇒ 1 (- z1 z2) procedure (- z) procedure (modulo -13 4) =⇒ 3 (- z1 z2 . . . ) optional procedure (remainder -13 4) =⇒ -1 (/ z1 z2) procedure (/ z) procedure (modulo 13 -4) =⇒ -3 (/ z1 z2 . . . ) optional procedure (remainder 13 -4) =⇒ 1

With two or more arguments, these procedures return the (modulo -13 -4) =⇒ -1 difference or quotient of their arguments, associating to the (remainder -13 -4) =⇒ -1 left. With one argument, however, they return the additive or multiplicative inverse of their argument. (remainder -13 -4.0) =⇒ -1.0 ; inexact 6. Standard procedures 23

(gcd n1 . . . ) library procedure (rationalize x y) library procedure (lcm n1 . . . ) library procedure These procedures return the greatest common divisor or Rationalize returns the simplest rational number differ- least common multiple of their arguments. The result is ing from x by no more than y. A rational number r1 is always non-negative. simpler than another rational number r2 if r1 = p1/q1 and (gcd 32 -36) =⇒ 4 r2 = p2/q2 (in lowest terms) and |p1| ≤ |p2| and |q1| ≤ |q2|. (gcd) =⇒ 0 Thus 3/5 is simpler than 4/7. Although not all rationals (lcm 32 -36) =⇒ 288 are comparable in this ordering (consider 2/7 and 3/5) any (lcm 32.0 -36) =⇒ 288.0 ; inexact interval contains a rational number that is simpler than ev- (lcm) =⇒ 1 ery other rational number in that interval (the simpler 2/5 lies between 2/7 and 3/5). Note that 0 = 0/1 is the sim- plest rational of all. (numerator q) procedure (denominator q) procedure (rationalize These procedures return the numerator or denominator of (inexact->exact .3) 1/10) =⇒ 1/3 ; exact their argument; the result is computed as if the argument (rationalize .3 1/10) =⇒ #i1/3 ; inexact was represented as a fraction in lowest terms. The denom- inator is always positive. The denominator of 0 is defined to be 1. (exp z) procedure (numerator (/ 6 4)) =⇒ 3 (log z) procedure (denominator (/ 6 4)) =⇒ 2 (sin z) procedure (denominator (cos z) procedure (exact->inexact (/ 6 4))) =⇒ 2.0 (tan z) procedure (asin z) procedure (floor x) procedure (acos z) procedure (ceiling x) procedure (atan z) procedure (truncate x) procedure (atan y x) procedure (round x) procedure These procedures are part of every implementation that supports general real numbers; they compute the usual These procedures return integers. Floor returns the transcendental functions. Log computes the natural log- largest integer not larger than x. Ceiling returns the arithm of z (not the base ten logarithm). Asin, acos, −1 − smallest integer not smaller than x. Truncate returns the and atan compute arcsine (sin ), arccosine (cos 1), and − integer closest to x whose absolute value is not larger than arctangent (tan 1), respectively. The two-argument vari- the absolute value of x. Round returns the closest inte- ant of atan computes (angle (make-rectangular x y)) ger to x, rounding to even when x is halfway between two (see below), even in implementations that don’t support integers. general complex numbers. Rationale: Round rounds to even for consistency with the de- In general, the mathematical functions log, arcsine, arc- fault rounding mode specified by the IEEE floating point stan- cosine, and arctangent are multiply defined. The value of dard. log z is defined to be the one whose imaginary part lies in the range from −π (exclusive) to π (inclusive). log 0 is un- Note: If the argument to one of these procedures is inexact, defined. With log defined this way, the values of sin−1 z, then the result will also be inexact. If an exact value is needed, cos−1 z, and tan−1 z are according to the following for- the result should be passed to the inexact->exact procedure. mulæ: (floor -4.3) =⇒ -5.0 sin−1 z = −i log(iz + p1 − z2) (ceiling -4.3) =⇒ -4.0 (truncate -4.3) =⇒ -4.0 cos−1 z = π/2 − sin−1 z (round -4.3) =⇒ -4.0 tan−1 z = (log(1 + iz) − log(1 − iz))/(2i) (floor 3.5) =⇒ 3.0 (ceiling 3.5) =⇒ 4.0 (truncate 3.5) =⇒ 3.0 The above specification follows [27], which in turn (round 3.5) =⇒ 4.0 ; inexact cites [19]; refer to these sources for more detailed discussion of branch cuts, boundary conditions, and implementation (round 7/2) =⇒ 4 ; exact of these functions. When it is possible these procedures (round 7) =⇒ 7 produce a real result from a real argument. 24 Revised5 Scheme

(sqrt z) procedure 6.2.6. Numerical input and output Returns the principal square root of z. The result will have either positive real part, or zero real part and non-negative (number->string z) procedure imaginary part. (number->string z radix) procedure

(expt z1 z2) procedure Radix must be an exact integer, either 2, 8, 10, or 16. If omitted, radix defaults to 10. The procedure number-> Returns z1 raised to the power z2. For z1 6= 0 string takes a number and a radix and returns as a string z z2 = ez2 log z1 an external representation of the given number in the given 1 radix such that z 0 is 1 if z = 0 and 0 otherwise. (let ((number number) (radix radix)) (make-rectangular x1 x2) procedure (eqv? number (make-polar x3 x4) procedure (string->number (number->string number (real-part z) procedure radix) (imag-part z) procedure radix))) (magnitude z) procedure (angle z) procedure is true. It is an error if no possible result makes this ex- These procedures are part of every implementation that pression true. supports general complex numbers. Suppose x1, x2, x3, If z is inexact, the radix is 10, and the above expression and x4 are real numbers and z is a complex number such can be satisfied by a result that contains a decimal point, that then the result contains a decimal point and is expressed ix4 z = x1 + x2i = x3 · e using the minimum number of digits (exclusive of exponent Then and trailing zeroes) needed to make the above expression true [3, 5]; otherwise the format of the result is unspecified. (make-rectangular x1 x2) =⇒ z (make-polar x3 x4) =⇒ z The result returned by number->string never contains an (real-part z) =⇒ x1 explicit radix prefix. (imag-part z) =⇒ x2 (magnitude z) =⇒ |x3| Note: The error case can occur only when z is not a complex (angle z) =⇒ xangle number or is a complex number with a non-rational real or imaginary part. where −π < xangle ≤ π with xangle = x4 + 2πn for some Rationale: If z is an inexact number represented using flonums, integer n. and the radix is 10, then the above expression is normally satis- Rationale: Magnitude is the same as abs for a real argu- fied by a result containing a decimal point. The unspecified case ment, but abs must be present in all implementations, whereas allows for infinities, NaNs, and non-flonum representations. magnitude need only be present in implementations that sup- port general complex numbers. (string->number string) procedure (string->number string radix) procedure (exact->inexact z) procedure Returns a number of the maximally precise representation (inexact->exact z) procedure expressed by the given string. Radix must be an exact Exact->inexact returns an inexact representation of z. integer, either 2, 8, 10, or 16. If supplied, radix is a default The value returned is the inexact number that is numeri- radix that may be overridden by an explicit radix prefix in cally closest to the argument. If an exact argument has no string (e.g. "#o177"). If radix is not supplied, then the reasonably close inexact equivalent, then a violation of an default radix is 10. If string is not a syntactically valid implementation restriction may be reported. notation for a number, then string->number returns #f. Inexact->exact returns an exact representation of z. The (string->number "100") =⇒ 100 value returned is the exact number that is numerically clos- (string->number "100" 16) =⇒ 256 est to the argument. If an inexact argument has no rea- (string->number "1e2") =⇒ 100.0 sonably close exact equivalent, then a violation of an im- (string->number "15##") =⇒ 1500.0 plementation restriction may be reported. Note: The domain of string->number may be restricted by These procedures implement the natural one-to-one corre- implementations in the following ways. String->number is per- spondence between exact and inexact integers throughout mitted to return #f whenever string contains an explicit radix an implementation-dependent range. See section 6.2.3. prefix. If all numbers supported by an implementation are real, 6. Standard procedures 25 then string->number is permitted to return #f whenever string (boolean? obj ) library procedure uses the polar or rectangular notations for complex numbers. If Boolean? returns #t if obj is either #t or #f and returns all numbers are integers, then string->number may return #f #f otherwise. whenever the fractional notation is used. If all numbers are exact, then string->number may return #f whenever an ex- (boolean? #f) =⇒ #t ponent marker or explicit exactness prefix is used, or if a # (boolean? 0) =⇒ #f appears in place of a digit. If all inexact numbers are integers, (boolean? ’()) =⇒ #f then string->number may return #f whenever a decimal point is used. 6.3.2. Pairs and lists

A pair (sometimes called a dotted pair) is a record structure 6.3. Other data types with two fields called the car and cdr fields (for historical reasons). Pairs are created by the procedure cons. The This section describes operations on some of Scheme’s non- car and cdr fields are accessed by the procedures car and numeric data types: booleans, pairs, lists, symbols, char- cdr. The car and cdr fields are assigned by the procedures acters, strings and vectors. set-car! and set-cdr!. Pairs are used primarily to represent lists. A list can be 6.3.1. Booleans defined recursively as either the empty list or a pair whose cdr is a list. More precisely, the set of lists is defined as The standard boolean objects for true and false are written the smallest set X such that as #t and #f. What really matters, though, are the objects that the Scheme conditional expressions (if, cond, and, • The empty list is in X . or, do) treat as true or false. The phrase “a true value” (or sometimes just “true”) means any object treated as • If list is in X , then any pair whose cdr field contains true by the conditional expressions, and the phrase “a false list is also in X . value” (or “false”) means any object treated as false by the conditional expressions. The objects in the car fields of successive pairs of a list are Of all the standard Scheme values, only #f counts as false the elements of the list. For example, a two-element list in conditional expressions. Except for #f, all standard is a pair whose car is the first element and whose cdr is a Scheme values, including #t, pairs, the empty list, sym- pair whose car is the second element and whose cdr is the bols, numbers, strings, vectors, and procedures, count as empty list. The length of a list is the number of elements, true. which is the same as the number of pairs. Note: Programmers accustomed to other dialects of Lisp The empty list is a special object of its own type (it is not should be aware that Scheme distinguishes both #f and the a pair); it has no elements and its length is zero. empty list from the symbol nil. Note: The above definitions imply that all lists have finite Boolean constants evaluate to themselves, so they do not length and are terminated by the empty list. need to be quoted in programs. The most general notation (external representation) for Scheme pairs is the “dotted” notation (c1 . c2) where c1 #t =⇒ #t is the value of the car field and c2 is the value of the cdr #f =⇒ #f field. For example (4 . 5) is a pair whose car is 4 and ’#f =⇒ #f whose cdr is 5. Note that (4 . 5) is the external repre- sentation of a pair, not an expression that evaluates to a pair. (not obj ) library procedure A more streamlined notation can be used for lists: the Not returns #t if obj is false, and returns #f otherwise. elements of the list are simply enclosed in parentheses and separated by spaces. The empty list is written () . For (not #t) =⇒ #f (not 3) =⇒ #f example, (not (list 3)) =⇒ #f (a b c d e) (not #f) =⇒ #t (not ’()) =⇒ #f and (not (list)) =⇒ #f (not ’nil) =⇒ #f (a . (b . (c . (d . (e . ()))))) 26 Revised5 Scheme are equivalent notations for a list of symbols. (car pair) procedure A chain of pairs not ending in the empty list is called an Returns the contents of the car field of pair. Note that it improper list. Note that an improper list is not a list. is an error to take the car of the empty list. The list and dotted notations can be combined to represent improper lists: (car ’(a b c)) =⇒ a (car ’((a) b c d)) =⇒ (a) (a b c . d) (car ’(1 . 2)) =⇒ 1 (car ’()) =⇒ error is equivalent to (a . (b . (c . d))) (cdr pair) procedure Whether a given pair is a list depends upon what is stored in the cdr field. When the set-cdr! procedure is used, an Returns the contents of the cdr field of pair. Note that it object can be a list one moment and not the next: is an error to take the cdr of the empty list. (define x (list ’a ’b ’c)) (cdr ’((a) b c d)) =⇒ (b c d) (define y x) (cdr ’(1 . 2)) =⇒ 2 y =⇒ (a b c) (cdr ’()) =⇒ error (list? y) =⇒ #t (set-cdr! x 4) =⇒ unspecified x =⇒ (a . 4) (set-car! pair obj ) procedure (eqv? x y) =⇒ #t obj pair y =⇒ (a . 4) Stores in the car field of . The value returned by (list? y) =⇒ #f set-car! is unspecified. unspecified (set-cdr! x x) =⇒ (define (f) (list ’not-a-constant-list)) (list? x) #f =⇒ (define (g) ’(constant-list)) Within literal expressions and representations of ob- (set-car! (f) 3) =⇒ unspecified jects read by the read procedure, the forms ’hdatumi, (set-car! (g) 3) =⇒ error `hdatumi, ,hdatumi, and ,@hdatumi denote two-ele- ment lists whose first elements are the symbols quote, (set-cdr! pair obj ) procedure quasiquote, unquote, and unquote-splicing, respec- tively. The second element in each case is hdatumi. This Stores obj in the cdr field of pair. The value returned by convention is supported so that arbitrary Scheme pro- set-cdr! is unspecified. grams may be represented as lists. That is, according to Scheme’s grammar, every hexpressioni is also a hdatumi (see section 7.1.2). Among other things, this permits the (caar pair) library procedure use of the read procedure to parse Scheme programs. See (cadr pair) library procedure . . section 3.3. . . (cdddar pair) library procedure (pair? obj ) procedure (cddddr pair) library procedure Pair? returns #t if obj is a pair, and otherwise returns #f. These procedures are compositions of car and cdr, where for example caddr could be defined by (pair? ’(a . b)) =⇒ #t (pair? ’(a b c)) =⇒ #t (define caddr (lambda (x) (car (cdr (cdr x))))). (pair? ’()) =⇒ #f (pair? ’#(a b)) =⇒ #f Arbitrary compositions, up to four deep, are provided. There are twenty-eight of these procedures in all.

(cons obj1 obj2) procedure (null? obj ) library procedure Returns a newly allocated pair whose car is obj1 and whose cdr is obj2. The pair is guaranteed to be different (in the Returns #t if obj is the empty list, otherwise returns #f. sense of eqv?) from every existing object. (cons ’a ’()) =⇒ (a) (list? obj ) library procedure (cons ’(a) ’(b c d)) =⇒ ((a) b c d) (cons "a" ’(b c)) =⇒ ("a" b c) Returns #t if obj is a list, otherwise returns #f. By defini- (cons ’a 3) =⇒ (a . 3) tion, all lists have finite length and are terminated by the (cons ’(a b) ’c) =⇒ ((a b) . c) empty list. 6. Standard procedures 27

(list? ’(a b c)) =⇒ #t (list-ref list k) library procedure (list? ’()) =⇒ #t Returns the kth element of list. (This is the same as the (list? ’(a . b)) =⇒ #f (let ((x (list ’a))) car of (list-tail list k).) It is an error if list has fewer (set-cdr! x x) than k elements. (list? x)) =⇒ #f (list-ref ’(a b c d) 2) =⇒ c (list-ref ’(a b c d) (inexact->exact (round 1.8))) (list obj . . . ) library procedure =⇒ c Returns a newly allocated list of its arguments.

(list ’a (+ 3 4) ’c) =⇒ (a 7 c) (memq obj list) library procedure (list) =⇒ () (memv obj list) library procedure (member obj list) library procedure These procedures return the first sublist of list whose car (length list) library procedure is obj , where the sublists of list are the non-empty lists Returns the length of list. returned by (list-tail list k) for k less than the length of list. If obj does not occur in list, then #f (not the empty (length ’(a b c)) =⇒ 3 Memq eq? obj (length ’(a (b) (c d e))) =⇒ 3 list) is returned. uses to compare with the (length ’()) =⇒ 0 elements of list, while memv uses eqv? and member uses equal?. (memq ’a ’(a b c)) =⇒ (a b c) (append list . . . ) library procedure (memq ’b ’(a b c)) =⇒ (b c) Returns a list consisting of the elements of the first list (memq ’a ’(b c d)) =⇒ #f followed by the elements of the other lists. (memq (list ’a) ’(b (a) c)) =⇒ #f (member (list ’a) (append ’(x) ’(y)) =⇒ (x y) ’(b (a) c)) =⇒ ((a) c) (append ’(a) ’(b c d)) =⇒ (a b c d) (memq 101 ’(100 101 102)) =⇒ unspecified (append ’(a (b)) ’((c))) =⇒ (a (b) (c)) (memv 101 ’(100 101 102)) =⇒ (101 102) The resulting list is always newly allocated, except that it shares structure with the last list argument. The last (assq obj alist) library procedure argument may actually be any object; an improper list (assv obj alist) library procedure results if the last argument is not a proper list. (assoc obj alist) library procedure

(append ’(a b) ’(c . d)) =⇒ (a b c . d) Alist (for “association list”) must be a list of pairs. These (append ’() ’a) =⇒ a procedures find the first pair in alist whose car field is obj , and returns that pair. If no pair in alist has obj as its car, then #f (not the empty list) is returned. Assq uses eq? to (reverse list) library procedure compare obj with the car fields of the pairs in alist, while Returns a newly allocated list consisting of the elements of assv uses eqv? and assoc uses equal?. list in reverse order. (define e ’((a 1) (b 2) (c 3))) (assq ’a e) =⇒ (a 1) (reverse ’(a b c)) =⇒ (c b a) (assq ’b e) =⇒ (b 2) (reverse ’(a (b c) d (e (f)))) (assq ’d e) =⇒ #f ((e (f)) d (b c) a) =⇒ (assq (list ’a) ’(((a)) ((b)) ((c)))) =⇒ #f (assoc (list ’a) ’(((a)) ((b)) ((c)))) list (list-tail k) library procedure =⇒ ((a)) Returns the sublist of list obtained by omitting the first k (assq 5 ’((2 3) (5 7) (11 13))) elements. It is an error if list has fewer than k elements. =⇒ unspecified List-tail could be defined by (assv 5 ’((2 3) (5 7) (11 13))) =⇒ (5 7) (define list-tail (lambda (x k) Rationale: Although they are ordinarily used as predicates, (if (zero? k) memq, memv, member, assq, assv, and assoc do not have question x marks in their names because they return useful values rather (list-tail (cdr x) (- k 1))))) than just #t or #f. 28 Revised5 Scheme

6.3.3. Symbols The following examples assume that the implementation’s standard case is lower case: Symbols are objects whose usefulness rests on the fact that two symbols are identical (in the sense of eqv?) if and only (symbol->string ’flying-fish) if their names are spelled the same way. This is exactly the =⇒ "flying-fish" property needed to represent identifiers in programs, and (symbol->string ’Martin) =⇒ "martin" so most implementations of Scheme use them internally for (symbol->string (string->symbol "Malvina")) that purpose. Symbols are useful for many other applica- =⇒ "Malvina" tions; 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 (string->symbol string) procedure rules for writing an identifier; see sections 2.1 and 7.1.1. Returns the symbol whose name is string. This procedure It is guaranteed that any symbol that has been returned as can create symbols with names containing special charac- part of a literal expression, or read using the read proce- ters or letters in the non-standard case, but it is usually dure, and subsequently written out using the write proce- a bad idea to create such symbols because in some imple- dure, will read back in as the identical symbol (in the sense mentations of Scheme they cannot be read as themselves. of eqv?). The string->symbol procedure, however, can See symbol->string. create symbols for which this write/read invariance may The following examples assume that the implementation’s not hold because their names contain special characters or standard case is lower case: letters in the non-standard case. Note: Some implementations of Scheme have a feature known (eq? ’mISSISSIppi ’mississippi) as “slashification” in order to guarantee write/read invariance =⇒ #t for all symbols, but historically the most important use of this (string->symbol "mISSISSIppi") feature has been to compensate for the lack of a string data =⇒ the symbol with name "mISSISSIppi" type. (eq? ’bitBlt (string->symbol "bitBlt")) =⇒ #f Some implementations also have “uninterned symbols”, which (eq? ’JollyWog defeat write/read invariance even in implementations with (string->symbol slashification, and also generate exceptions to the rule that two (symbol->string ’JollyWog))) symbols are the same if and only if their names are spelled the =⇒ #t same. (string=? "K. Harper, M.D." (symbol->string (string->symbol "K. Harper, M.D."))) (symbol? obj ) procedure =⇒ #t Returns #t if obj is a symbol, otherwise returns #f. (symbol? ’foo) =⇒ #t 6.3.4. Characters (symbol? (car ’(a b))) =⇒ #t (symbol? "bar") =⇒ #f Characters are objects that represent printed characters (symbol? ’nil) =⇒ #t such as letters and digits. Characters are written using the (symbol? ’()) =⇒ #f notation #\hcharacteri or #\hcharacter namei. For exam- (symbol? #f) =⇒ #f ple:

(symbol->string symbol) procedure Returns the name of symbol as a string. If the symbol was #\a ; lower case letter part of an object returned as the value of a literal expres- #\A ; upper case letter sion (section 4.1.2) or by a call to the read procedure, and #\( ; left parenthesis its name contains alphabetic characters, then the string #\ ; the space character returned will contain characters in the implementation’s #\space ; the preferred way to write a space preferred standard case—some implementations will prefer #\newline ; the newline character upper case, others lower case. If the symbol was returned by string->symbol, the case of characters in the string Case is significant in #\hcharacteri, but not in #\hcharacter returned will be the same as the case in the string that namei. If hcharacteri in #\hcharacteri is alphabetic, then was passed to string->symbol. It is an error to apply the character following hcharacteri must be a delimiter mutation procedures like string-set! to strings returned character such as a space or parenthesis. This rule resolves by this procedure. the ambiguous case where, for example, the sequence of 6. Standard procedures 29 characters “#\space” could be taken to be either a repre- (char-alphabetic? char) library procedure sentation of the space character or a representation of the (char-numeric? char) library procedure character “#\s” followed by a representation of the symbol (char-whitespace? char) library procedure “pace.” (char-upper-case? letter) library procedure (char-lower-case? letter) library procedure Characters written in the #\ notation are self-evaluating. That is, they do not have to be quoted in programs. These procedures return #t if their arguments are alpha- betic, numeric, whitespace, upper case, or lower case char- Some of the procedures that operate on characters ignore acters, respectively, otherwise they return #f. The follow- the difference between upper case and lower case. The pro- cedures that ignore case have “-ci” (for “case insensitive”) ing remarks, which are specific to the ASCII character set, are intended only as a guide: The alphabetic characters are embedded in their names. the 52 upper and lower case letters. The numeric charac- ters are the ten decimal digits. The whitespace characters (char? obj ) procedure are space, tab, line feed, form feed, and carriage return. Returns #t if obj is a character, otherwise returns #f. (char->integer char) procedure (integer->char n) procedure (char=? char1 char2) procedure (charinteger returns an exact inte- (char>? char1 char2) procedure ger representation of the character. Given an exact inte- (char<=? char1 char2) procedure ger that is the image of a character under char->integer, (char>=? char1 char2) procedure integer->char returns that character. These procedures These procedures impose a total ordering on the set of implement order-preserving isomorphisms between the set characters. It is guaranteed that under this ordering: of characters under the char<=? ordering and some subset of the integers under the <= ordering. That is, if

• The upper case characters are in order. For example, (char<=? a b) =⇒ #t and (<= x y) =⇒ #t (charchar, then • The lower case characters are in order. For example, (<= (char->integer a) (charinteger b)) =⇒ #t • The digits are in order. For example, (charchar ) #\9) returns #t. x (integer->char y)) =⇒ #t • Either all the digits precede all the upper case letters, or vice versa. (char-upcase char) library procedure • Either all the digits precede all the lower case letters, (char-downcase char) library procedure or vice versa. These procedures return a character char2 such that (char-ci=? char char2). In addition, if char is alpha- Some implementations may generalize these procedures to betic, then the result of char-upcase is upper case and take more than two arguments, as with the corresponding the result of char-downcase is lower case. numerical predicates.

6.3.5. Strings (char-ci=? char1 char2) library procedure (char-ci? char1 char2) library procedure as sequences of characters enclosed within doublequotes (char-ci<=? char1 char2) library procedure ("). A doublequote can be written inside a string only by (char-ci>=? char1 char2) library procedure escaping it with a backslash (\), as in

These procedures are similar to char=? et cetera, but they "The word \"recursion\" has many meanings." treat upper case and lower case letters as the same. For example, (char-ci=? #\A #\a) returns #t. Some imple- A backslash can be written inside a string only by escaping mentations may generalize these procedures to take more it with another backslash. Scheme does not specify the than two arguments, as with the corresponding numerical effect of a backslash within a string that is not followed by predicates. a doublequote or backslash. 30 Revised5 Scheme

A string constant may continue from one line to the next, (string=? string1 string2) library procedure but the exact contents of such a string are unspecified. (string-ci=? string1 string2) library procedure The length of a string is the number of characters that it Returns #t if the two strings are the same length and con- contains. This number is an exact, non-negative integer tain the same characters in the same positions, otherwise that is fixed when the string is created. The valid indexes returns #f. String-ci=? treats upper and lower case let- of a string are the exact non-negative integers less than ters as though they were the same character, but string=? the length of the string. The first character of a string has treats upper and lower case as distinct characters. index 0, the second has index 1, and so on.

In phrases such as “the characters of string beginning with (string? string1 string2) library procedure that the index start is inclusive and the index end is ex- (string<=? string1 string2) library procedure clusive. Thus if start and end are the same index, a null (string>=? string1 string2) library procedure substring is referred to, and if start is zero and end is the (string-ci? string1 string2) library procedure (string-ci<=? string string ) library procedure Some of the procedures that operate on strings ignore the 1 2 (string-ci>=? string string ) library procedure difference between upper and lower case. The versions that 1 2 ignore case have “-ci” (for “case insensitive”) embedded These procedures are the lexicographic extensions to in their names. strings of the corresponding orderings on characters. For example, string

(string-ref string k) procedure Returns a newly allocated string whose characters form the concatenation of the given strings. k must be a valid index of string. String-ref returns character k of string using zero-origin indexing. (string->list string) library procedure (list->string list) library procedure (string-set! string k char) procedure String->list returns a newly allocated list of the charac- k must be a valid index of string. String-set! stores char ters that make up the given string. List->string returns in element k of string and returns an unspecified value. a newly allocated string formed from the characters in the (define (f) (make-string 3 #\*)) list list, which must be a list of characters. String->list (define (g) "***") and list->string are inverses so far as equal? is con- (string-set! (f) 0 #\?) =⇒ unspecified cerned. (string-set! (g) 0 #\?) =⇒ error (string-set! (symbol->string ’immutable) (string-copy string) library procedure 0 #\?) =⇒ error Returns a newly allocated copy of the given string. 6. Standard procedures 31

(string-fill! string char) library procedure (vector-ref ’#(1 1 2 3 5 8 13 21) Stores char in every element of the given string and returns 5) =⇒ 8 an unspecified value. (vector-ref ’#(1 1 2 3 5 8 13 21) (let ((i (round (* 2 (acos -1))))) 6.3.6. Vectors (if (inexact? i) (inexact->exact i) Vectors are heterogenous structures whose elements are in- i))) dexed by integers. A vector typically occupies less space =⇒ 13 than a list of the same length, and the average time re- quired to access a randomly chosen element is typically (vector-set! vector k obj ) procedure less for the vector than for the list. k must be a valid index of vector. Vector-set! stores obj The length of a vector is the number of elements that it in element k of vector. The value returned by vector-set! contains. This number is a non-negative integer that is is unspecified. fixed when the vector is created. The valid indexes of a vector are the exact non-negative integers less than the (let ((vec (vector 0 ’(2 2 2 2) "Anna"))) length of the vector. The first element in a vector is indexed (vector-set! vec 1 ’("Sue" "Sue")) by zero, and the last element is indexed by one less than vec) the length of the vector. =⇒ #(0 ("Sue" "Sue") "Anna") Vectors are written using the notation #(obj . . . ). For (vector-set! ’#(0 1 2) 1 "doe") example, a vector of length 3 containing the number zero =⇒ error ; constant vector in element 0, the list (2 2 2 2) in element 1, and the string "Anna" in element 2 can be written as following: (vector->list vector) library procedure #(0 (2 2 2 2) "Anna") (list->vector list) library procedure Note that this is the external representation of a vector, not Vector->list returns a newly allocated list of the objects an expression evaluating to a vector. Like list constants, contained in the elements of vector. List->vector returns vector constants must be quoted: a newly created vector initialized to the elements of the list ’#(0 (2 2 2 2) "Anna") list. =⇒ #(0 (2 2 2 2) "Anna") (vector->list ’#(dah dah didah)) =⇒ (dah dah didah) (vector? obj ) procedure (list->vector ’(dididit dah)) =⇒ #(dididit dah) Returns #t if obj is a vector, otherwise returns #f. (vector-fill! vector fill) library procedure (make-vector k) procedure (make-vector k fill) procedure Stores fill in every element of vector. The value returned by vector-fill! is unspecified. 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 unspeci- 6.4. Control features fied. This chapter describes various primitive procedures which (vector obj . . . ) library procedure control the flow of program execution in special ways. The Returns a newly allocated vector whose elements contain procedure? predicate is also described here. the given arguments. Analogous to list. (vector ’a ’b ’c) =⇒ #(a b c) (procedure? obj ) procedure Returns #t if obj is a procedure, otherwise returns #f. (vector-length vector) procedure (procedure? car) =⇒ #t Returns the number of elements in vector as an exact in- (procedure? ’car) =⇒ #f teger. (procedure? (lambda (x) (* x x))) =⇒ #t (procedure? ’(lambda (x) (* x x))) (vector-ref vector k) procedure =⇒ #f k must be a valid index of vector. Vector-ref returns the (call-with-current-continuation procedure?) contents of element k of vector. =⇒ #t 32 Revised5 Scheme

(apply proc arg1 . . . args) procedure computed and returned. The value of the promise is cached Proc must be a procedure and args must be a list. Calls (or “memoized”) so that if it is forced a second time, the previously computed value is returned. proc with the elements of the list (append (list arg1 . . . ) args) as the actual arguments. (force (delay (+ 1 2))) =⇒ 3 (let ((p (delay (+ 1 2)))) (apply + (list 3 4)) =⇒ 7 (list (force p) (force p))) =⇒ (3 3) (define compose (lambda (f g) (define a-stream (lambda args (letrec ((next (f (apply g args))))) (lambda (n) (cons n (delay (next (+ n 1))))))) ((compose sqrt *) 12 75) =⇒ 30 (next 0))) (define head car)

(map proc list1 list2 . . . ) library procedure (define tail (lambda (stream) (force (cdr stream)))) The lists must be lists, and proc must be a procedure taking as many arguments as there are lists and returning a single (head (tail (tail a-stream))) value. If more than one list is given, then they must all =⇒ 2 be the same length. Map applies proc element-wise to the elements of the lists and returns a list of the results, in Force and delay are mainly intended for programs written order. The dynamic order in which proc is applied to the in functional style. The following examples should not be elements of the lists is unspecified. considered to illustrate good programming style, but they illustrate the property that only one value is computed for (map cadr ’((a b) (d e) (g h))) a promise, no matter how many times it is forced. =⇒ (b e h) (define count 0) (map (lambda (n) (expt n n)) (define p ’(1 2 3 4 5)) (delay (begin (set! count (+ count 1)) =⇒ (1 4 27 256 3125) (if (> count x) count (map + ’(1 2 3) ’(4 5 6)) =⇒ (5 7 9) (force p))))) (define x 5) (let ((count 0)) p =⇒ a promise (map (lambda (ignored) (force p) =⇒ 6 (set! count (+ count 1)) p =⇒ a promise, still count) (begin (set! x 10) ’(a b))) =⇒ (1 2) or (2 1) (force p)) =⇒ 6 Here is a possible implementation of delay and force. Promises are implemented here as procedures of no argu- (for-each proc list1 list2 . . . ) library procedure ments, and force simply calls its argument: The arguments to for-each are like the arguments to map, (define force but for-each calls proc for its side effects rather than for (lambda (object) its values. Unlike map, for-each is guaranteed to call proc (object))) on the elements of the lists in order from the first ele- ment(s) to the last, and the value returned by for-each is We define the expression unspecified. (delay hexpressioni) (let ((v (make-vector 5))) to have the same meaning as the procedure call (for-each (lambda (i) (vector-set! v i (* i i))) (make-promise (lambda () hexpressioni)) ’(0 1 2 3 4)) v) =⇒ #(0 1 4 9 16) as follows

(force promise) library procedure (define-syntax delay (syntax-rules () Forces the value of promise (see delay, section 4.2.5). If no ((delay expression) value has been computed for the promise, then a value is (make-promise (lambda () expression))))), 6. Standard procedures 33 where make-promise is defined as follows: call-with-current-continuation. Except for continua- tions created by the call-with-values procedure, all con- (define make-promise (lambda (proc) tinuations take exactly one value. The effect of passing no (let ((result-ready? #f) value or more than one value to continuations that were (result #f)) not created by call-with-values is unspecified. (lambda () The escape procedure that is passed to proc has unlimited (if result-ready? extent just like any other procedure in Scheme. It may be result stored in variables or data structures and may be called as (let ((x (proc))) (if result-ready? many times as desired. result The following examples show only the most common ways (begin (set! result-ready? #t) in which call-with-current-continuation is used. If (set! result x) all real uses were as simple as these examples, there result)))))))) would be no need for a procedure with the power of Rationale: A promise may refer to its own value, as in the call-with-current-continuation. last example above. Forcing such a promise may cause the (call-with-current-continuation promise to be forced a second time before the value of the first (lambda (exit) force has been computed. This complicates the definition of (for-each (lambda (x) make-promise. (if (negative? x) Various extensions to this semantics of delay and force (exit x))) ’(54 0 37 -3 245 19)) are supported in some implementations: #t)) =⇒ -3

• Calling force on an object that is not a promise may (define list-length simply return the object. (lambda (obj) (call-with-current-continuation • It may be the case that there is no means by which (lambda (return) a promise can be operationally distinguished from its (letrec ((r forced value. That is, expressions like the following (lambda (obj) may evaluate to either #t or to #f, depending on the (cond ((null? obj) 0) implementation: ((pair? obj) (+ (r (cdr obj)) 1)) (eqv? (delay 1) 1) =⇒ unspecified (else (return #f)))))) (pair? (delay (cons 1 2))) =⇒ unspecified (r obj)))))) (list-length ’(1 2 3 4)) =⇒ 4 • Some implementations may implement “implicit forc- ing,” where the value of a promise is forced by primi- (list-length ’(a b . c)) =⇒ #f tive procedures like cdr and +: Rationale: (+ (delay (* 3 7)) 13) =⇒ 34 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. (call-with-current-continuation proc) procedure Whenever a Scheme expression is evaluated there is a contin- Proc must be a procedure of one argument. The procedure uation wanting the result of the expression. The continuation call-with-current-continuation packages up the cur- represents an entire (default) future for the computation. If the rent continuation (see the rationale below) as an “escape expression is evaluated at top level, for example, then the con- procedure” and passes it as an argument to proc. The es- tinuation might take the result, print it on the screen, prompt cape procedure is a Scheme procedure that, if it is later for the next input, evaluate it, and so on forever. Most of the called, will abandon whatever continuation is in effect at time the continuation includes actions specified by user code, that later time and will instead use the continuation that as in a continuation that will take the result, multiply it by the was in effect when the escape procedure was created. Call- value stored in a local variable, add seven, and give the answer ing the escape procedure may cause the invocation of before to the top level continuation to be printed. Normally these and after thunks installed using dynamic-wind. ubiquitous continuations are hidden behind the scenes and pro- grammers do not think much about them. On rare occasions, The escape procedure accepts the same number of ar- however, a programmer may need to deal with continuations ex- guments as the continuation to the original call to plicitly. Call-with-current-continuation allows Scheme pro- 34 Revised5 Scheme grammers to do that by creating a procedure that acts just like Scheme, because of call-with-current-continuation, the current continuation. the dynamic extent of a call may not be a single, connected Most programming languages incorporate one or more special- time period. It is defined as follows: purpose escape constructs with names like exit, return, or even goto. In 1965, however, [16] invented a • The dynamic extent is entered when execution of the general purpose escape operator called the J-operator. John body of the called procedure begins. Reynolds [24] described a simpler but equally powerful con- • The dynamic extent is also entered when exe- struct in 1972. The catch special form described by Sussman cution is not within the dynamic extent and a and Steele in the 1975 report on Scheme is exactly the same as continuation is invoked that was captured (using Reynolds’s construct, though its name came from a less general call-with-current-continuation) during the dy- construct in MacLisp. Several Scheme implementors noticed namic extent. that the full power of the catch construct could be provided by a procedure instead of by a special syntactic construct, and the • It is exited when the called procedure returns. name call-with-current-continuation was coined in 1982. This name is descriptive, but opinions differ on the merits of • It is also exited when execution is within the dynamic such a long name, and some people use the name call/cc in- extent and a continuation is invoked that was captured stead. while not within the dynamic extent.

If a second call to dynamic-wind occurs within the dynamic (values obj . . .) procedure extent of the call to thunk and then a continuation is in- Delivers all of its arguments to its continuation. Except voked in such a way that the afters from these two invoca- for continuations created by the call-with-values pro- tions of dynamic-wind are both to be called, then the after cedure, all continuations take exactly one value. Values associated with the second (inner) call to dynamic-wind is might be defined as follows: called first. (define (values . things) (call-with-current-continuation If a second call to dynamic-wind occurs within the dy- (lambda (cont) (apply cont things)))) namic extent of the call to thunk and then a continua- tion is invoked in such a way that the befores from these two invocations of dynamic-wind are both to be called, then the before associated with the first (outer) call to (call-with-values producer consumer) procedure dynamic-wind is called first. Calls its producer argument with no values and a contin- If invoking a continuation requires calling the before from uation that, when passed some values, calls the consumer one call to dynamic-wind and the after from another, then procedure with those values as arguments. The continua- the after is called first. tion for the call to consumer is the continuation of the call to call-with-values. The effect of using a captured continuation to enter or exit the dynamic extent of a call to before or after is undefined. (call-with-values (lambda () (values 4 5)) (lambda (a b) b)) (let ((path ’()) =⇒ 5 (c #f)) (let ((add (lambda (s) (call-with-values * -) =⇒ -1 (set! path (cons s path))))) (dynamic-wind (lambda () (add ’connect)) (lambda () (dynamic-wind before thunk after) procedure (add (call-with-current-continuation (lambda (c0) Calls thunk without arguments, returning the result(s) of (set! c c0) this call. Before and after are called, also without ar- ’talk1)))) guments, as required by the following rules (note that (lambda () (add ’disconnect))) in the absence of calls to continuations captured using (if (< (length path) 4) call-with-current-continuation the three arguments (c ’talk2) are called once each, in order). Before is called whenever (reverse path)))) execution enters the dynamic extent of the call to thunk and after is called whenever it exits that dynamic extent. =⇒ (connect talk1 disconnect connect talk2 disconnect) The dynamic extent of a procedure call is the period be- tween when the call is initiated and when it returns. In 6. Standard procedures 35

6.5. Eval 6.6. Input and output

6.6.1. Ports (eval expression environment-specifier) procedure Ports represent input and output devices. To Scheme, an Evaluates expression in the specified environment and re- input port is a Scheme object that can deliver characters turns its value. Expression must be a valid Scheme expres- upon command, while an output port is a Scheme object sion represented as data, and environment-specifier must that can accept characters. be a value returned by one of the three procedures de- scribed below. Implementations may extend eval to allow non-expression programs (definitions) as the first argument (call-with-input-file string proc) library procedure and to allow other values as environments, with the re- (call-with-output-file string proc) library procedure striction that eval is not allowed to create new bindings String should be a string naming a file, and proc in the environments associated with null-environment or should be a procedure that accepts one argument. For scheme-report-environment. call-with-input-file, the file should already exist; for (eval ’(* 7 3) (scheme-report-environment 5)) call-with-output-file, the effect is unspecified if the =⇒ 21 file already exists. These procedures call proc with one ar- gument: the port obtained by opening the named file for (let ((f (eval ’(lambda (f x) (f x x)) input or output. If the file cannot be opened, an error is (null-environment 5)))) signalled. If proc returns, then the port is closed automati- (f + 10)) cally and the value(s) yielded by the proc is(are) returned. =⇒ 20 If proc does not return, then the port will not be closed automatically unless it is possible to prove that the port will never again be used for a read or write operation. (scheme-report-environment version) procedure Rationale: Because Scheme’s escape procedures have un- (null-environment version) procedure limited extent, it is possible to escape from the current con- Version must be the exact integer 5, corresponding to this tinuation but later to escape back in. If implementations revision of the Scheme report (the Revised5 Report on were permitted to close the port on any escape from the Scheme). Scheme-report-environment returns a specifier current continuation, then it would be impossible to write for an environment that is empty except for all bindings de- portable code using both call-with-current-continuation fined in this report that are either required or both optional and call-with-input-file or call-with-output-file. and supported by the implementation. Null-environment returns a specifier for an environment that is empty except (input-port? obj ) procedure for the (syntactic) bindings for all syntactic keywords de- (output-port? obj ) procedure fined in this report that are either required or both optional and supported by the implementation. Returns #t if obj is an input port or output port respec- tively, otherwise returns #f. Other values of version can be used to specify environments matching past revisions of this report, but their support is not required. An implementation will signal an error if (current-input-port) procedure version is neither 5 nor another value supported by the (current-output-port) procedure implementation. Returns the current default input or output port. The effect of assigning (through the use of eval) a vari- able bound in a scheme-report-environment (for exam- (with-input-from-file string thunk) ple car) is unspecified. Thus the environments specified by scheme-report-environment may be immutable. optional procedure (with-output-to-file string thunk) optional procedure (interaction-environment) optional procedure String should be a string naming a file, and proc should be This procedure returns a specifier for the environment that a procedure of no arguments. For with-input-from-file, contains implementation-defined bindings, typically a su- the file should already exist; for with-output-to-file, perset of those listed in the report. The intent is that this the effect is unspecified if the file already exists. The procedure will return the environment in which the imple- file is opened for input or output, an input or output mentation would evaluate expressions dynamically typed port connected to it is made the default value returned by the user. by current-input-port or current-output-port (and is 36 Revised5 Scheme used by (read), (write obj ), and so forth), and the thunk (read-char) procedure is called with no arguments. When the thunk returns, (read-char port) procedure the port is closed and the previous default is restored. Returns the next character available from the input port, With-input-from-file and with-output-to-file re- updating the port to point to the following character. If turn(s) the value(s) yielded by thunk. If an escape pro- no more characters are available, an end of file object is cedure is used to escape from the continuation of these returned. Port may be omitted, in which case it defaults procedures, their behavior is implementation dependent. to the value returned by current-input-port.

(open-input-file filename) procedure (peek-char) procedure Takes a string naming an existing file and returns an input (peek-char port) procedure port capable of delivering characters from the file. If the Returns the next character available from the input port, file cannot be opened, an error is signalled. without updating the port to point to the following char- acter. If no more characters are available, an end of file Port (open-output-file filename) procedure object is returned. may be omitted, in which case it defaults to the value returned by current-input-port. Takes a string naming an output file to be created and returns an output port capable of writing characters to a Note: The value returned by a call to peek-char is the same as new file by that name. If the file cannot be opened, an the value that would have been returned by a call to read-char error is signalled. If a file with the given name already with the same port. The only difference is that the very next call exists, the effect is unspecified. to read-char or peek-char on that port will return the value returned by the preceding call to peek-char. In particular, a call to peek-char on an interactive port will hang waiting for (close-input-port port) procedure input whenever a call to read-char would have hung. (close-output-port port) procedure Closes the file associated with port, rendering the port in- (eof-object? obj ) procedure capable of delivering or accepting characters. These rou- Returns #t if obj is an end of file object, otherwise returns tines have no effect if the file has already been closed. The #f. The precise set of end of file objects will vary among value returned is unspecified. implementations, but in any case no end of file object will ever be an object that can be read in using read. 6.6.2. Input (char-ready?) procedure (read) library procedure (char-ready? port) procedure (read port) library procedure Returns #t if a character is ready on the input port and Read converts external representations of Scheme objects returns #f otherwise. If char-ready returns #t then the port into the objects themselves. That is, it is a parser for the next read-char operation on the given is guaranteed port nonterminal hdatumi (see sections 7.1.2 and 6.3.2). Read not to hang. If the is at end of file then char-ready? Port returns the next object parsable from the given input port, returns #t. may be omitted, in which case it defaults updating port to point to the first character past the end to the value returned by current-input-port. of the external representation of the object. Rationale: Char-ready? exists to make it possible for a pro- If an end of file is encountered in the input before any char- gram to accept characters from interactive ports without getting acters are found that can begin an object, then an end of stuck waiting for input. Any input editors associated with such file object is returned. The port remains open, and fur- ports must ensure that characters whose existence has been as- ther attempts to read will also return an end of file object. serted by char-ready? cannot be rubbed out. If char-ready? If an end of file is encountered after the beginning of an were to return #f at end of file, a port at end of file would object’s external representation, but the external represen- be indistinguishable from an interactive port that has no ready tation is incomplete and therefore not parsable, an error is characters. signalled. The port argument may be omitted, in which case it de- 6.6.3. Output faults to the value returned by current-input-port. It is an error to read from a closed port. 6. Standard procedures 37

(write obj ) library procedure sequentially. It is unspecified whether the results of the (write obj port) library procedure expressions are printed. The load procedure does not Writes a written representation of obj to the given port. affect the values returned by current-input-port and Strings that appear in the written representation are en- current-output-port. Load returns an unspecified value. closed in doublequotes, and within those strings backslash Rationale: For portability, load must operate on source files. and doublequote characters are escaped by backslashes. Its operation on other kinds of files necessarily varies among Character objects are written using the #\ notation. Write implementations. returns an unspecified value. The port argument may be omitted, in which case it defaults to the value returned by current-output-port. (transcript-on filename) optional procedure (transcript-off) optional procedure (display obj ) library procedure Filename must be a string naming an output file to be cre- (display obj port) library procedure ated. The effect of transcript-on is to open the named Writes a representation of obj to the given port. Strings file for output, and to cause a transcript of subsequent that appear in the written representation are not enclosed interaction between the user and the Scheme system to in doublequotes, and no characters are escaped within be written to the file. The transcript is ended by a call those strings. Character objects appear in the represen- to transcript-off, which closes the transcript file. Only tation as if written by write-char instead of by write. one transcript may be in progress at any time, though some Display returns an unspecified value. The port argument implementations may relax this restriction. The values re- may be omitted, in which case it defaults to the value re- turned by these procedures are unspecified. turned 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 charac- ters in symbols.

(newline) library procedure (newline port) library procedure 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 omit- ted, in which case it defaults to the value returned by current-output-port.

(write-char char) procedure (write-char char port) procedure Writes the character char (not an external representa- tion of the character) to the given port and returns an unspecified value. The port argument may be omit- ted, in which case it defaults to the value returned by current-output-port.

6.6.4. System interface

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

(load filename) optional procedure Filename should be a string naming an existing file con- taining Scheme source code. The load procedure reads ex- pressions and definitions from the file and evaluates them 38 Revised5 Scheme

7. Formal syntax and semantics hsyntactic keywordi −→ hexpression keywordi | else | => | define This chapter provides formal descriptions of what has al- | unquote | unquote-splicing ready been described informally in previous chapters of this hexpression keywordi −→ quote | lambda | if report. | set! | begin | cond | and | or | case | let | let* | letrec | do | delay 7.1. Formal syntax | quasiquote

hvariablei −→ hany hidentifieri that isn’t This section provides a formal syntax for Scheme written also a hsyntactic keywordii in an extended BNF. All spaces in the grammar are for legibility. Case is insignif- hbooleani −→ #t | #f icant; for example, #x1A and #X1a are equivalent. hemptyi hcharacteri −→ #\ hany characteri stands for the empty string. | #\ hcharacter namei The following extensions to BNF are used to make the de- hcharacter namei −→ space | newline scription more concise: hthingi* means zero or more occur- rences of hthingi; and hthingi+ means at least one hthingi. hstringi −→ " hstring elementi* " hstring elementi −→ hany character other than " or \i | \" | \\ 7.1.1. Lexical structure hnumberi −→ hnum 2i| hnum 8i This section describes how individual tokens (identifiers, | hnum 10i| hnum 16i numbers, etc.) are formed from sequences of characters. The following sections describe how expressions and pro- The following rules for hnum Ri, hcomplex Ri, hreal Ri, grams are formed from sequences of tokens. hureal Ri, huinteger Ri, and hprefix Ri should be repli- hIntertoken spacei may occur on either side of any token, cated for R = 2, 8, 10, and 16. There are no rules for but not within a token. hdecimal 2i, hdecimal 8i, and hdecimal 16i, which means Tokens which require implicit termination (identifiers, that numbers containing decimal points or exponents must numbers, characters, and dot) may be terminated by any be in decimal radix. hdelimiteri, but not necessarily by anything else. hnum Ri −→ hprefix Ri hcomplex Ri The following five characters are reserved for future exten- hcomplex Ri −→ hreal Ri | hreal Ri @ hreal Ri sions to the language: [ ] { } | | hreal Ri + hureal Ri i | hreal Ri - hureal Ri i | hreal Ri + i | hreal Ri - i htokeni −→ hidentifieri | hbooleani | hnumberi | + hureal Ri i | - hureal Ri i | + i | - i | hcharacteri | hstringi hreal Ri −→ hsigni hureal Ri | ( | ) | #( | ’ | ` | , | ,@ | . hureal Ri −→ huinteger Ri hdelimiteri −→ hwhitespacei | ( | ) | " | ; | huinteger Ri / huinteger Ri hwhitespacei −→ hspace or newlinei | hdecimal Ri hcommenti −→ ; hall subsequent characters up to a hdecimal 10i −→ huinteger 10i hsuffixi line breaki | . hdigit 10i+ #* hsuffixi hatmospherei −→ hwhitespacei | hcommenti | hdigit 10i+ . hdigit 10i* #* hsuffixi hintertoken spacei −→ hatmospherei* | hdigit 10i+ #+ . #* hsuffixi huinteger Ri −→ hdigit Ri+ #* hidentifieri −→ hinitiali hsubsequenti* hprefix Ri −→ hradix Ri hexactnessi | hpeculiar identifieri | hexactnessi hradix Ri hinitiali −→ hletteri | hspecial initiali hletteri −→ a | b | c | ... | z hsuffixi −→ hemptyi hspecial initiali −→ ! | $ | % | & | * | / | : | < | = | hexponent markeri hsigni hdigit 10i+ | > | ? | ^ | _ | ~ hexponent markeri −→ e | s | f | d | l hsubsequenti −→ hinitiali | hdigiti hsigni −→ hemptyi | + | - | hspecial subsequenti hexactnessi −→ hemptyi | #i | #e hdigiti −→ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 hradix 2i −→ #b hspecial subsequenti −→ + | - | . | @ hradix 8i −→ #o hpeculiar identifieri −→ + | - | ... hradix 10i −→ hemptyi | #d 7. Formal syntax and semantics 39 hradix 16i −→ #x hassignmenti −→ (set! hvariablei hexpressioni) hdigit 2i −→ 0 | 1 hdigit 8i −→ 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 hderived expressioni −→ hdigit 10i −→ hdigiti (cond hcond clausei+) hdigit 16i −→ hdigit 10i | a | b | c | d | e | f | (cond hcond clausei* (else hsequencei)) | (case hexpressioni hcase clausei+) 7.1.2. External representations | (case hexpressioni hDatumi is what the read procedure (section 6.6.2) suc- hcase clausei* (else )) cessfully parses. Note that any string that parses as an hsequencei (and ) hexpressioni will also parse as a hdatumi. | htesti* | (or htesti*) hdatumi −→ hsimple datumi | hcompound datumi | (let (hbinding speci*) hbodyi) hsimple datumi −→ hbooleani | hnumberi | (let hvariablei (hbinding speci*) hbodyi) | hcharacteri | hstringi | hsymboli | (let* (hbinding speci*) hbodyi) hsymboli −→ hidentifieri | (letrec (hbinding speci*) hbodyi) hcompound datumi −→ hlisti | hvectori | (begin hsequencei) hlisti −→ (hdatumi*) | (hdatumi+ . hdatumi) | (do (hiteration speci*) | habbreviationi (htesti hdo resulti) habbreviationi −→ habbrev prefixi hdatumi hcommandi*) habbrev prefixi −→ ’ | ` | , | ,@ | (delay hexpressioni) hvectori −→ #(hdatumi*) | hquasiquotationi

hcond clausei −→ (htesti hsequencei) 7.1.3. Expressions | (htesti) ( => ) hexpressioni −→ hvariablei | htesti hrecipienti hrecipienti −→ hexpressioni | hliterali hcase clausei −→ ((hdatumi*) hsequencei) | hprocedure calli ( ) | hlambda expressioni hbinding speci −→ hvariablei hexpressioni hiteration speci −→ (hvariablei hiniti hstepi) | hconditionali ( ) | hassignmenti | hvariablei hiniti | hderived expressioni hiniti −→ hexpressioni hstepi −→ hexpressioni | hmacro usei | hmacro blocki hdo resulti −→ hsequencei | hemptyi hmacro usei −→ (hkeywordi hdatumi*) hliterali −→ hquotationi | hself-evaluatingi hself-evaluatingi −→ hbooleani | hnumberi hkeywordi −→ hidentifieri | hcharacteri | hstringi hquotationi −→ ’hdatumi | (quote hdatumi) hmacro blocki −→ (let-syntax ( ) ) hprocedure calli −→ (hoperatori hoperandi*) hsyntax speci* hbodyi | (letrec-syntax (hsyntax speci*) hbodyi) hoperatori −→ hexpressioni ( ) hoperandi −→ hexpressioni hsyntax speci −→ hkeywordi htransformer speci hlambda expressioni −→ (lambda hformalsi hbodyi) hformalsi −→ (hvariablei*) | hvariablei + | (hvariablei . hvariablei) 7.1.4. Quasiquotations hbodyi −→ hdefinitioni* hsequencei hsequencei −→ hcommandi* hexpressioni The following grammar for quasiquote expressions is not hcommandi −→ hexpressioni context-free. It is presented as a recipe for generating an infinite number of production rules. Imagine a copy of the hconditionali −→ (if htesti hconsequenti halternatei) following rules for D = 1, 2, 3, . . .. D keeps track of the htesti −→ hexpressioni nesting depth. hconsequenti −→ hexpressioni halternatei −→ hexpressioni | hemptyi hquasiquotationi −→ hquasiquotation 1i hqq template 0i −→ hexpressioni 40 Revised5 Scheme hquasiquotation Di −→ `hqq template Di hcommand or definitioni −→ hcommandi | (quasiquote hqq template Di) | hdefinitioni hqq template Di −→ hsimple datumi | hsyntax definitioni | hlist qq template Di | (begin hcommand or definitioni+) | hvector qq template Di hdefinitioni −→ (define hvariablei hexpressioni) | hunquotation Di | (define (hvariablei hdef formalsi) hbodyi) hlist qq template Di −→ (hqq template or splice Di*) | (begin hdefinitioni*) | (hqq template or splice Di+ . hqq template Di) hdef formalsi −→ hvariablei* | ’hqq template Di | hvariablei* . hvariablei | hquasiquotation D + 1i hsyntax definitioni −→ hvector qq template Di −→ #(hqq template or splice Di*) (define-syntax hkeywordi htransformer speci) hunquotation Di −→ ,hqq template D − 1i | (unquote hqq template D − 1i) hqq template or splice Di −→ hqq template Di | hsplicing unquotation Di 7.2. Formal semantics hsplicing unquotation Di −→ ,@hqq template D − 1i | (unquote-splicing hqq template D − 1i) This section provides a formal denotational semantics for the primitive expressions of Scheme and selected built-in In hquasiquotationis, a hlist qq template Di can some- procedures. The concepts and notation used here are de- times be confused with either an hunquotation Di or scribed in [29]; the notation is summarized below: a hsplicing unquotation Di. The interpretation as an h . . . i sequence formation hunquotationi or hsplicing unquotation Di takes prece- s ↓ k kth member of the sequence s (1-based) dence. #s length of sequence s s § t concatenation of sequences s and t 7.1.5. Transformers s † k drop the first k members of sequence s t → a, b McCarthy conditional “if t then a else b” htransformer speci −→ ρ[x/i] substitution “ρ with x for i” (syntax-rules (hidentifieri*) hsyntax rulei*) x in D injection of x into domain D hsyntax rulei −→ (hpatterni htemplatei) x | D projection of x to domain D hpatterni −→ hpattern identifieri The reason that expression continuations take sequences | (hpatterni*) of values instead of single values is to simplify the formal + | (hpatterni . hpatterni) treatment of procedure calls and multiple return values. | (hpatterni* hpatterni hellipsisi) | #(hpatterni*) The boolean flag associated with pairs, vectors, and strings | #(hpatterni* hpatterni hellipsisi) will be true for mutable objects and false for immutable | hpattern datumi objects. hpattern datumi −→ hstringi The order of evaluation within a call is unspecified. We | hcharacteri mimic that here by applying arbitrary permutations per- | hbooleani mute and unpermute, which must be inverses, to the argu- | hnumberi ments in a call before and after they are evaluated. This is htemplatei −→ hpattern identifieri not quite right since it suggests, incorrectly, that the order | (htemplate elementi*) of evaluation is constant throughout a program (for any | (htemplate elementi+ . htemplatei) given number of arguments), but it is a closer approxima- | #(htemplate elementi*) tion to the intended semantics than a left-to-right evalua- | htemplate datumi tion would be. htemplate elementi −→ htemplatei The storage allocator new is implementation-dependent, | htemplatei hellipsisi but it must obey the following axiom: if new σ ∈ L, then htemplate datumi −→ hpattern datumi σ (new σ | L) ↓ 2 = false. hpattern identifieri −→ hany identifier except ...i The definition of K is omitted because an accurate defini- hellipsisi −→ hthe identifier ...i tion of K would complicate the semantics without being very interesting. If P is a program in which all variables are defined before 7.1.6. Programs and definitions being referenced or assigned, then the meaning of P is hprogrami −→ hcommand or definitioni* E[[((lambda (I*) P’) hundefinedi . . . )]] 7. Formal syntax and semantics 41

0 where I* is the sequence of variables defined in P, P is the E[[I]] = λρκ . hold (lookup ρ I) sequence of expressions obtained by replacing every defini- (single(λ .  = undefined → tion in P by an assignment, hundefinedi is an expression wrong “undefined variable”, that evaluates to undefined, and E is the semantic function send  κ)) that assigns meaning to expressions. E[[(E0 E*)]] = λρκ . E*(permute(hE0i § E*)) ρ 7.2.1. Abstract syntax (λ* . ((λ* . applicate (* ↓ 1) (* † 1) κ) (unpermute *))) K ∈ Con constants, including quotations I ∈ Ide identifiers (variables) E[[(lambda (I*) Γ* E0)]] = E ∈ Exp expressions λρκ . λσ . new σ ∈ L → Γ ∈ Com = Exp commands send (hnew σ | L, λ*κ0 . #* = #I* → 0 0 0 0 tievals(λα* . (λρ . C[[Γ*]]ρ (E[[E0]]ρ κ )) Exp −→ K | I | (E0 E*) (extends ρ I* α*)) | (lambda (I*) Γ* E ) 0 *, . | (lambda (I* I) Γ* E0) wrong “wrong number of arguments”i | (lambda I Γ* E0) in E) | (if E0 E1 E2) | (if E0 E1) κ | (set! I E) (update (new σ | L) unspecified σ), wrong “out of memory” σ

E[[(lambda (I* . I) Γ* E0)]] = 7.2.2. Domain equations λρκ . λσ . new σ ∈ L → ∈ send (hnew σ | L, α L locations 0 ν ∈ N natural numbers λ*κ . #* ≥ #I* → tievalsrest T = {false, true} booleans 0 0 0 0 (λα* . (λρ . C[[Γ*]]ρ (E[[E0]]ρ κ )) Q symbols (extends ρ (I* § hIi) α*)) H characters * R numbers (#I*), Ep = L × L × T pairs wrong “too few arguments”i in E) Ev = L* × T vectors κ Es = L* × T strings (update (new σ | L) unspecified σ), M = {false, true, null, undefined, unspecified} wrong “out of memory” σ

miscellaneous E[[(lambda I Γ* E0)]] = E[[(lambda (. I) Γ* E0)]] φ ∈ F = L × (E* → K → C) procedure values E[[(if E0 E1 E2)]] =  ∈ E = Q + H + R + Ep + Ev + Es + M + F λρκ . E[[E0]] ρ (single (λ . truish  → E[[E1]]ρκ, expressed values E[[E2]]ρκ)) σ ∈ S = L → (E × T) stores ρ ∈ U = Ide → L environments E[[(if E0 E1)]] = θ ∈ C = S → A command continuations λρκ . E[[E0]] ρ (single (λ . truish  → E[[E1]]ρκ, κ ∈ K = E* → C expression continuations send unspecified κ)) A answers Here and elsewhere, any expressed value other than undefined X errors may be used in place of unspecified. E[[(set! I E)]] = 7.2.3. Semantic functions λρκ . E[[E]] ρ (single(λ . assign (lookup ρ I)  K : Con → E (send unspecified κ))) E : Exp → U → K → C E*[[ ]] = λρκ . κh i E* : Exp* → U → K → C C : Com* → U → C → C E*[[E0 E*]] = λρκ . E[[E0]] ρ (single(λ0 . E*[[E*]] ρ (λ* . κ (h0i § *)))) Definition of K deliberately omitted. C[[ ]] = λρθ . θ

E[[K]] = λρκ . send (K[[K]]) κ C[[Γ0 Γ*]] = λρθ . E[[Γ0]] ρ (λ* . C[[Γ*]]ρθ) 42 Revised5 Scheme

7.2.4. Auxiliary functions list : E* → K → C list = lookup : U → Ide → L λ*κ . #* = 0 → send null κ, lookup = λρI . ρI list (* † 1)(single(λ . consh* ↓ 1, iκ)) extends : U → Ide* → L* → U cons : E* → K → C extends = cons = λρI*α* . #I* = 0 → ρ, twoarg (λ12κσ . new σ ∈ L → 0 0 extends (ρ[(α* ↓ 1)/(I* ↓ 1)]) (I* † 1) (α* † 1) (λσ . new σ ∈ L → 0 wrong : X → C [implementation-dependent] send (hnew σ | L, new σ | L, truei in E) send : E → K → C κ 0 0 send = λκ . κhi (update(new σ | L)2σ ), wrong “out of memory”σ0) single : (E → C) → K (update(new σ | L)1σ), single = wrong “out of memory”σ) λψ* . #* = 1 → ψ(* ↓ 1), wrong “wrong number of return values” less : E* → K → C new : S → (L + {error}) [implementation-dependent] less = twoarg (λ12κ . (1 ∈ R ∧ 2 ∈ R) → hold : L → K → C send (1 | R < 2 | R → true, false)κ, hold = λακσ . send (σα ↓ 1)κσ wrong “non-numeric argument to <”) assign : L → E → C → C add : E* → K → C assign = λαθσ . θ(update ασ) add = update : L → E → S → S twoarg (λ12κ . (1 ∈ R ∧ 2 ∈ R) → update = λασ . σ[h, truei/α] send ((1 | R + 2 | R) in E)κ, wrong “non-numeric argument to +”) tievals : (L* → C) → E* → C tievals = car : E* → K → C λψ*σ . #* = 0 → ψh iσ, car = ∈ new σ L → tievals (λα* . ψ(hnew σ | Li § α*)) onearg (λκ .  ∈ Ep → hold ( | Ep ↓ 1)κ, (* † 1) wrong “non-pair argument to car”) (update(new σ | L)(* ↓ 1)σ), wrong “out of memory”σ cdr : E* → K → C [similar to car] tievalsrest : (L* → C) → E* → N → C setcar : E* → K → C tievalsrest = setcar = λψ*ν . list (dropfirst *ν) twoarg (λ12κ . 1 ∈ Ep → (single(λ . tievals ψ ((takefirst *ν) § hi))) (1 | Ep ↓ 3) → assign (1 | Ep ↓ 1) 2 dropfirst = λln . n = 0 → l, dropfirst (l † 1)(n − 1) (send unspecified κ), takefirst = λln . n = 0 → h i, hl ↓ 1i § (takefirst (l † 1)(n − 1)) wrong “immutable argument to set-car!”, wrong “non-pair argument to set-car!”) truish : E → T truish = λ .  = false → false, true eqv : E* → K → C eqv = permute : Exp* → Exp* [implementation-dependent] twoarg (λ12κ . (1 ∈ M ∧ 2 ∈ M) → unpermute : E* → E* [inverse of permute] send (1 | M = 2 | M → true, false)κ, (1 ∈ Q ∧ 2 ∈ Q) → applicate : E → E* → K → C send (1 | Q = 2 | Q → true, false)κ, applicate = (1 ∈ H ∧ 2 ∈ H) → λ*κ .  ∈ F → ( | F ↓ 2)*κ, wrong “bad procedure” send (1 | H = 2 | H → true, false)κ, onearg : (E → K → C) → (E* → K → C) (1 ∈ R ∧ 2 ∈ R) → onearg = send (1 | R = 2 | R → true, false)κ, λζ*κ . #* = 1 → ζ(* ↓ 1)κ, (1 ∈ Ep ∧ 2 ∈ Ep) → wrong “wrong number of arguments” send ((λp1p2 . ((p1 ↓ 1) = (p2 ↓ 1)∧ (p1 ↓ 2) = (p2 ↓ 2)) → true, twoarg : (E → E → K → C) → (E* → K → C) false) twoarg = (1 | Ep) λζ*κ . #* = 2 → ζ(* ↓ 1)(* ↓ 2)κ, (2 | Ep)) wrong “wrong number of arguments” κ, 7. Formal syntax and semantics 43

(1 ∈ Ev ∧ 2 ∈ Ev) → . . . , ((cond (test)) test) (1 ∈ Es ∧ 2 ∈ Es) → . . . , ((cond (test) clause1 clause2 ...) (1 ∈ F ∧ 2 ∈ F) → (let ((temp test)) send ((1 | F ↓ 1) = (2 | F ↓ 1) → true, false) (if temp κ, temp send false κ) (cond clause1 clause2 ...)))) ((cond (test result1 result2 ...)) apply : E* → K → C (if test (begin result1 result2 ...))) apply = ((cond (test result1 result2 ...) twoarg (λ12κ . 1 ∈ F → valueslist h2i(λ* . applicate 1*κ), clause1 clause2 ...) wrong “bad procedure argument to apply”) (if test valueslist : E* → K → C (begin result1 result2 ...) valueslist = (cond clause1 clause2 ...))))) onearg (λκ .  ∈ Ep → cdrhi (define-syntax case (λ* . valueslist (syntax-rules (else) * ((case (key ...) (λ* . carhi(single(λ . κ(hi § *))))), clauses ...)  = null → κh i, (let ((atom-key (key ...))) wrong “non-list argument to values-list”) (case atom-key clauses ...))) cwcc : E* → K → C [call-with-current-continuation] ((case key cwcc = (else result1 result2 ...)) (begin result1 result2 ...)) onearg (λκ .  ∈ F → (λσ . new σ ∈ L → ((case key applicate  ((atoms ...) result1 result2 ...)) hhnew σ | L, λ*κ0 . κ*i in Ei (if (memv key ’(atoms ...)) κ (begin result1 result2 ...))) (update (new σ | L) ((case key unspecified ((atoms ...) result1 result2 ...) σ), clause clauses ...) wrong “out of memory” σ), (if (memv key ’(atoms ...)) wrong “bad procedure argument”) (begin result1 result2 ...) (case key clause clauses ...))))) values : E* → K → C values = λ*κ . κ* (define-syntax and cwv : E* → K → C [call-with-values] (syntax-rules () cwv = ((and) #t) twoarg (λ12κ . applicate 1h i(λ* . applicate 2 *)) ((and test) test) ((and test1 test2 ...) (if test1 (and test2 ...) #f)))) 7.3. Derived expression types (define-syntax or This section gives macro definitions for the derived expres- (syntax-rules () sion types in terms of the primitive expression types (lit- ((or) #f) eral, variable, call, lambda, if, set!). See section 6.4 for ((or test) test) a possible definition of delay. ((or test1 test2 ...) (let ((x test1)) (define-syntax cond (if x x (or test2 ...)))))) (syntax-rules (else =>) ((cond (else result1 result2 ...)) (begin result1 result2 ...)) (define-syntax let ((cond (test => result)) (syntax-rules () (let ((temp test)) ((let ((name val) ...) body1 body2 ...) (if temp (result temp)))) ((lambda (name ...) body1 body2 ...) ((cond (test => result) clause1 clause2 ...) val ...)) (let ((temp test)) ((let tag ((name val) ...) body1 body2 ...) (if temp ((letrec ((tag (lambda (name ...) (result temp) body1 body2 ...))) (cond clause1 clause2 ...)))) tag) 44 Revised5 Scheme

val ...)))) in the body of a lambda expression. In any case, note that these rules apply only if the body of the begin contains no definitions. (define-syntax let* (syntax-rules () (define-syntax begin ((let* () body1 body2 ...) (syntax-rules () (let () body1 body2 ...)) ((begin exp) ((let* ((name1 val1) (name2 val2) ...) exp) body1 body2 ...) ((begin exp1 exp2 ...) (let ((name1 val1)) (let ((x exp1)) (let* ((name2 val2) ...) (begin exp2 ...))))) body1 body2 ...)))))

The following definition of do uses a trick to expand the The following letrec macro uses the symbol variable clauses. As with letrec above, an auxiliary macro in place of an expression which returns something that would also work. The expression (if #f #f) is used to when stored in a location makes it an error to try to ob- obtain an unspecific value. tain the value stored in the location (no such expression is defined in Scheme). A trick is used to generate the tempo- (define-syntax do (syntax-rules () rary names needed to avoid specifying the order in which ((do ((var init step ...) ...) the values are evaluated. This could also be accomplished (test expr ...) by using an auxiliary macro. command ...) (define-syntax letrec (letrec (syntax-rules () ((loop ((letrec ((var1 init1) ...) body ...) (lambda (var ...) (letrec "generate temp names" (if test (var1 ...) (begin () (if #f #f) ((var1 init1) ...) expr ...) body ...)) (begin ((letrec "generate temp names" command () ... (temp1 ...) (loop (do "step" var step ...) ((var1 init1) ...) ...)))))) body ...) (loop init ...))) (let ((var1 ) ...) ((do "step" x) (let ((temp1 init1) ...) x) (set! var1 temp1) ((do "step" x y) ... y))) body ...))) ((letrec "generate temp names" (x y ...) (temp ...) ((var1 init1) ...) body ...) (letrec "generate temp names" (y ...) (newtemp temp ...) ((var1 init1) ...) body ...))))

(define-syntax begin (syntax-rules () ((begin exp ...) ((lambda () exp ...)))))

The following alternative expansion for begin does not make use of the ability to write more than one expression Example 45

NOTES EXAMPLE

Integrate-system integrates the system

Language changes 0 yk = fk(y1, y2, . . . , yn), k = 1, . . . , n This section enumerates the changes that have been made of differential equations with the method of Runge-Kutta. to Scheme since the “Revised4 report” [6] was published. The parameter system-derivative is a function that • The report is now a superset of the IEEE standard takes a system state (a vector of values for the state vari- ables y1, . . . , yn) and produces a system derivative (the val- for Scheme [13]: implementations that conform to the 0 0 report will also conform to the standard. This required ues y1, . . . , yn). The parameter initial-state provides the following changes: an initial system state, and h is an initial guess for the length of the integration step. – The empty list is now required to count as true. The value returned by integrate-system is an infinite – The classification of features as essential or stream of system states. inessential has been removed. There are now (define integrate-system three classes of built-in procedures: primitive, li- (lambda (system-derivative initial-state h) brary, and optional. The optional procedures are (let ((next (runge-kutta-4 system-derivative h))) load, with-input-from-file, with-output- (letrec ((states to-file, transcript-on, transcript-off, and (cons initial-state interaction-environment, and - and / with (delay (map-streams next more than two arguments. None of these are in states))))) the IEEE standard. states)))) – Programs are allowed to redefine built-in proce- Runge-Kutta-4 takes a function, f, that produces a system dures. Doing so will not change the behavior of derivative from a system state. Runge-Kutta-4 produces other built-in procedures. a function that takes a system state and produces a new system state. • Port has been added to the list of disjoint types. (define runge-kutta-4 • The macro appendix has been removed. High-level (lambda (f h) macros are now part of the main body of the report. (let ((*h (scale-vector h)) The rewrite rules for derived expressions have been (*2 (scale-vector 2)) replaced with macro definitions. There are no reserved (*1/2 (scale-vector (/ 1 2))) identifiers. (*1/6 (scale-vector (/ 1 6)))) (lambda (y) • Syntax-rules now allows vector patterns. ;; y is a system state (let* ((k0 (*h (f y))) • Multiple-value returns, eval, and dynamic-wind have (k1 (*h (f (add-vectors y (*1/2 k0))))) been added. (k2 (*h (f (add-vectors y (*1/2 k1))))) (k3 (*h (f (add-vectors y k2))))) • The calls that are required to be implemented in a (add-vectors y properly tail-recursive fashion are defined explicitly. (*1/6 (add-vectors k0 (*2 k1) • ‘@’ can be used within identifiers. ‘|’ is reserved for (*2 k2) possible future extensions. k3))))))))

(define elementwise ADDITIONAL MATERIAL (lambda (f) (lambda vectors The Internet Scheme Repository at (generate-vector (vector-length (car vectors)) http://www.cs.indiana.edu/scheme-repository/ (lambda (i) contains an extensive Scheme bibliography, as well as pa- (apply f (map (lambda (v) (vector-ref v i)) pers, programs, implementations, and other material re- vectors))))))) lated to Scheme. (define generate-vector (lambda (size proc) 46 Revised5 Scheme

(let ((ans (make-vector size))) REFERENCES (letrec ((loop (lambda (i) (cond ((= i size) ans) [1] Harold Abelson and Gerald Jay Sussman with Julie (else Sussman. Structure and Interpretation of Computer (vector-set! ans i (proc i)) Programs, second edition. MIT Press, Cambridge, (loop (+ i 1))))))) 1996. (loop 0))))) [2] Alan Bawden and Jonathan Rees. Syntactic closures. (define add-vectors (elementwise +)) In Proceedings of the 1988 ACM Symposium on Lisp and Functional Programming, pages 86–95. (define scale-vector (lambda (s) [3] Robert G. Burger and R. Kent Dybvig. Printing (elementwise (lambda (x) (* x s))))) floating-point numbers quickly and accurately. In Proceedings of the ACM SIGPLAN ’96 Conference Map-streams is analogous to map: it applies its first argu- on Programming Language Design and Implementa- ment (a procedure) to all the elements of its second argu- tion, pages 108–116. ment (a stream). (define map-streams [4] William Clinger, editor. The revised revised report (lambda (f s) on Scheme, or an uncommon Lisp. MIT Artificial (cons (f (head s)) Intelligence Memo 848, August 1985. Also published (delay (map-streams f (tail s)))))) as Computer Science Department Technical Report 174, Indiana University, June 1985. Infinite streams are implemented as pairs whose car holds the first element of the stream and whose cdr holds a [5] William Clinger. How to read floating point numbers promise to deliver the rest of the stream. accurately. In Proceedings of the ACM SIGPLAN (define head car) ’90 Conference on Programming Language Design (define tail and Implementation, pages 92–101. Proceedings pub- (lambda (stream) (force (cdr stream)))) lished as SIGPLAN Notices 25(6), June 1990.

[6] William Clinger and Jonathan Rees, editors. The The following illustrates the use of integrate-system in revised4 report on the algorithmic language Scheme. integrating the system In ACM Lisp Pointers 4(3), pages 1–55, 1991.

dvC vC C = −iL − [7] William Clinger and Jonathan Rees. Macros that dt R work. In Proceedings of the 1991 ACM Conference on Principles of Programming Languages, pages 155– di L L = v 162. dt C which models a damped oscillator. [8] William Clinger. Proper Tail Recursion and Space Efficiency. To appear in Proceedings of the 1998 ACM (define damped-oscillator Conference on Programming Language Design and (lambda (R L C) Implementation, June 1998. (lambda (state) (let ((Vc (vector-ref state 0)) [9] R. Kent Dybvig, Robert Hieb, and Carl Bruggeman. (Il (vector-ref state 1))) Syntactic abstraction in Scheme. Lisp and Symbolic (vector (- 0 (+ (/ Vc (* R C)) (/ Il C))) Computation 5(4):295–326, 1993. (/ Vc L)))))) [10] Carol Fessenden, William Clinger, Daniel P. Fried- (define the-states (integrate-system man, and Christopher Haynes. Scheme 311 version 4 (damped-oscillator 10000 1000 .001) reference manual. Indiana University Computer Sci- ’#(1 0) ence Technical Report 137, February 1983. Super- .01)) seded by [11].

[11] D. Friedman, C. Haynes, E. Kohlbecker, and M. Wand. Scheme 84 interim reference manual. Indi- ana University Computer Science Technical Report 153, January 1985. References 47

[12] IEEE Standard 754-1985. IEEE Standard for Binary [25] Guy Lewis Steele Jr. and Gerald Jay Sussman. The Floating-Point Arithmetic. IEEE, New York, 1985. revised report on Scheme, a dialect of Lisp. MIT Ar- tificial Intelligence Memo 452, January 1978. [13] IEEE Standard 1178-1990. IEEE Standard for the Scheme Programming Language. IEEE, New York, [26] Guy Lewis Steele Jr. Rabbit: a compiler for Scheme. 1991. MIT Artificial Intelligence Laboratory Technical Re- port 474, May 1978. [14] Eugene E. Kohlbecker Jr. Syntactic Extensions in the Programming Language Lisp. PhD thesis, Indi- [27] Guy Lewis Steele Jr. Common Lisp: The Language, ana University, August 1986. second edition. Digital Press, Burlington MA, 1990.

[15] Eugene E. Kohlbecker Jr., Daniel P. Friedman, [28] Gerald Jay Sussman and Guy Lewis Steele Jr. Matthias Felleisen, and Bruce Duba. Hygienic macro Scheme: an interpreter for extended lambda calcu- expansion. In Proceedings of the 1986 ACM Con- lus. MIT Artificial Intelligence Memo 349, December ference on Lisp and Functional Programming, pages 1975. 151–161. [29] Joseph E. Stoy. Denotational Semantics: The Scott- Strachey Approach to Programming Language The- [16] Peter Landin. A correspondence between Algol 60 ory. MIT Press, Cambridge, 1977. and Church’s lambda notation: Part I. Communica- tions of the ACM 8(2):89–101, February 1965. [30] Texas Instruments, Inc. TI Scheme Language Ref- erence Manual. Preliminary version 1.0, November [17] MIT Department of Electrical Engineering and Com- 1985. puter Science. Scheme manual, seventh edition. September 1984.

[18] Peter Naur et al. Revised report on the algorith- mic language Algol 60. Communications of the ACM 6(1):1–17, January 1963.

[19] Paul Penfield, Jr. Principal values and branch cuts in complex APL. In APL ’81 Conference Proceed- ings, pages 248–256. ACM SIGAPL, San Fran- cisco, September 1981. Proceedings published as APL Quote Quad 12(1), ACM, September 1981.

[20] Kent M. Pitman. The revised MacLisp manual (Sat- urday evening edition). MIT Laboratory for Com- puter Science Technical Report 295, May 1983.

[21] 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 Sym- posium on Lisp and Functional Programming, pages 114–122.

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

[23] Jonathan Rees and William Clinger, editors. The revised3 report on the algorithmic language Scheme. In ACM SIGPLAN Notices 21(12), pages 37–79, De- cember 1986.

[24] John Reynolds. Definitional interpreters for higher order programming languages. In ACM Conference Proceedings, pages 717–740. ACM, 1972. 48 Revised5 Scheme ALPHABETIC INDEX OF DEFINITIONS OF CONCEPTS, KEYWORDS, AND PROCEDURES

The principal entry for each term, procedure, or keyword is case 10; 43 listed first, separated from the other entries by a semicolon. catch 34 cdddar 26 ! 5 cddddr 26 ’ 8; 26 cdr 26 * 22 ceiling 23 + 22; 5, 42 char->integer 29 , 13; 26 char-alphabetic? 29 ,@ 13 char-ci<=? 29 - 22; 5 char-ci 5 char-ci=? 29 ... 5; 14 char-ci>=? 29 / 22 char-ci>? 29 ; 5 char-downcase 29 < 21; 42 char-lower-case? 29 <= 21 char-numeric? 29 = 21; 22 char-ready? 36 => 10 char-upcase 29 > 21 char-upper-case? 29 >= 21 char-whitespace? 29 ? 4 char<=? 29 ‘ 13 char=? 29 acos 23 char>? 29 and 11; 43 char? 29; 6 angle 24 close-input-port 36 append 27 close-output-port 36 apply 32; 8, 43 combination 9 asin 23 comma 13 assoc 27 comment 5; 38 assq 27 complex? 21; 19 assv 27 cond 10; 15, 43 atan 23 cons 26 #b 21; 38 constant 7 backquote 13 continuation 33 begin 12; 16, 44 cos 23 binding 6 current-input-port 35 binding construct 6 current-output-port 35 boolean? 25; 6 bound 6 #d 21 define 16; 14 caar 26 define-syntax 17 cadr 26 definition 16 call 9 delay 13; 32 call by need 13 denominator 23 call-with-current-continuation 33; 8, 34, 43 display 37 call-with-input-file 35 do 12; 44 call-with-output-file 35 dotted pair 25 call-with-values 34; 8, 43 dynamic-wind 34; 33 call/cc 34 car 26; 42 #e 21; 38 Index 49 else 10 library 3 empty list 25; 6, 26 library procedure 17 eof-object? 36 list 27 eq? 18; 10 list->string 30 equal? 19 list->vector 31 equivalence predicate 17 list-ref 27 eqv? 17; 7, 10, 42 list-tail 27 error 4 list? 26 escape procedure 33 load 37 eval 35; 8 location 7 even? 22 log 23 exact 17 exact->inexact 24 macro 13 exact? 21 macro keyword 13 exactness 19 macro transformer 13 exp 23 macro use 13 expt 24 magnitude 24 make-polar 24 #f 25 make-rectangular 24 false 6; 25 make-string 30 floor 23 make-vector 31 for-each 32 map 32 force 32; 13 max 22 member 27 gcd 23 memq 27 memv 27 hygienic 13 min 22 modulo 22 #i 21; 38 mutable 7 identifier 5; 6, 28, 38 if 10; 41 negative? 22 imag-part 24 newline 37 immutable 7 nil 25 implementation restriction 4; 20 not 25 improper list 26 null-environment 35 inexact 17 null? 26 inexact->exact 24; 20 number 19 inexact? 21 number->string 24 initial environment 17 number? 21; 6, 19 input-port? 35 numerator 23 integer->char 29 numerical types 19 integer? 21; 19 interaction-environment 35 #o 21; 38 internal definition 16 object 3 odd? 22 keyword 13; 38 open-input-file 36 open-output-file 36 lambda 9; 16, 41 optional 3 lazy evaluation 13 or 11; 43 lcm 23 output-port? 35 length 27; 20 let 11; 12, 15, 16, 43 pair 25 let* 11; 16, 44 pair? 26; 6 let-syntax 14; 16 peek-char 36 letrec 11; 16, 44 port 35 letrec-syntax 14; 16 port? 6 50 Revised5 Scheme positive? 22 syntax definition 17 predicate 17 syntax-rules 14; 17 procedure call 9 procedure? 31; 6 #t 25 promise 13; 32 tail call 7 proper tail recursion 7 tan 23 token 38 quasiquote 13; 26 top level environment 17; 6 quote 8; 26 transcript-off 37 quotient 22 transcript-on 37 true 6; 10, 25 rational? 21; 19 truncate 23 rationalize 23 type 6 read 36; 26, 39 read-char 36 unbound 6; 8, 16 real-part 24 unquote 13; 26 real? 21; 19 unquote-splicing 13; 26 referentially transparent 13 unspecified 4 region 6; 10, 11, 12 remainder 22 valid indexes 30; 31 reverse 27 values 34; 9 round 23 variable 6; 5, 8, 38 vector 31 scheme-report-environment 35 vector->list 31 set! 10; 16, 41 vector-fill! 31 set-car! 26 vector-length 31; 20 set-cdr! 26 vector-ref 31 setcar 42 vector-set! 31 simplest rational 23 vector? 31; 6 sin 23 sqrt 24 whitespace 5 string 30 with-input-from-file 35 string->list 30 with-output-to-file 35 string->number 24 write 37; 13 string->symbol 28 write-char 37 string-append 30 string-ci<=? 30 #x 21; 39 string-ci=? 30 string-ci>? 30 string-copy 30 string-fill! 31 string-length 30; 20 string-ref 30 string-set! 30; 28 string<=? 30 string=? 30 string>? 30 string? 30; 6 substring 30 symbol->string 28; 7 symbol? 28; 6 syntactic keyword 6; 5, 13, 38