This is r4rs.info, produced by Makeinfo version 3.12a from r4rs.texi. INFO-DIR-SECTION Scheme Programming START-INFO-DIR-ENTRY * r4rs: (r4rs). Revised(4) Report on Scheme END-INFO-DIR-ENTRY  File: r4rs.info, Node: Top, Next: Introduction, Prev: (dir), Up: (dir) Revised(4) Report on the Algorithmic Language Scheme 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 ALGOL 60 Converted to TeXinfo by A. Jaffer (jaffer@ai.mit.edu) We intend this report to belong to the entire Scheme community, and so we grant permission to copy it in whole or in part without fee. In particular, we encourage implementors of Scheme to use this report as a starting point for manuals and other documentation, modifying it as necessary. 2 November 1991 Summary ******* The report gives a defining description of the programming language Scheme. Scheme is a statically scoped and properly tail-recursive dialect of the Lisp programming language invented by Guy Lewis Steele Jr. and Gerald Jay Sussman. It was designed to have an exceptionally clear and simple semantics and few different ways to form expressions. A wide variety of programming paradigms, including imperative, functional, and message passing styles, find convenient expression in Scheme. The introduction offers a brief history of the language and of the report. The first three chapters present the fundamental ideas of the language and describe the notational conventions used for describing the language and for writing programs in the language. *Note Expressions:: and *Note Program structure:: describe the syntax and semantics of expressions, programs, and definitions. *Note Standard procedures:: describes Scheme's built-in procedures, which include all of the language's data manipulation and input/output primitives. *Note Formal syntax and semantics:: provides a formal syntax for Scheme written in extended BNF, along with a formal denotational semantics. An example of the use of the language follows the formal syntax and semantics. The appendix describes a macro facility that may be used to extend the syntax of Scheme. The report concludes with a bibliography and an alphabetic index. For the LaTeX source for this report, as well as postscript and dvi renditions, see [R4RS] in the bibliography. * Menu: * Introduction:: * Overview of Scheme:: * Lexical conventions:: * Basic concepts:: * Expressions:: * Program structure:: * Standard procedures:: * Formal syntax and semantics:: * Notes:: * Example:: * Macros:: Appendix * Bibliography and references:: * Index:: Alphabetic index of definitions of concepts, keywords, and procedures  File: r4rs.info, Node: Introduction, Next: Overview of Scheme, Prev: Top, Up: Top Description of the Language *************************** Introduction ************ * Menu: * History:: * Background:: * Acknowledgements::  File: r4rs.info, Node: History, Next: Background, Prev: Introduction, Up: Introduction History ======= Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. Scheme demonstrates that a very small number of rules for forming expressions, with no restrictions on how they are composed, suffice to form a practical and efficient programming language that is flexible enough to support most of the major programming paradigms in use today. Scheme was one of the first programming languages to incorporate first class procedures as in the lambda calculus, thereby proving the usefulness of static scope rules and block structure in a dynamically typed language. Scheme was the first major dialect of Lisp to distinguish procedures from lambda expressions and symbols, to use a single lexical environment for all variables, and to evaluate the operator position of a procedure call in the same way as an operand position. By relying entirely on procedure calls to express iteration, Scheme emphasized the fact that tail-recursive procedure calls are essentially goto's that pass arguments. Scheme was the first widely used programming language to embrace first class escape procedures, from which all previously known sequential control structures can be synthesized. More recently, building upon the design of generic arithmetic in Common Lisp, Scheme introduced the concept of exact and inexact numbers. With the appendix to this report Scheme becomes the first programming language to support hygienic macros, which permit the syntax of a block-structured language to be extended reliably.  File: r4rs.info, Node: Background, Next: Acknowledgements, Prev: History, Up: Introduction Background ========== The first description of Scheme was written in 1975 [SCHEME75]. A revised report [SCHEME78] appeared in 1978, which described the evolution of the language as its MIT implementation was upgraded to support an innovative compiler [RABBIT]. Three distinct projects began in 1981 and 1982 to use variants of Scheme for courses at MIT, Yale, and Indiana University [REES82] [MITSCHEME] [SCHEME311]. An introductory computer science textbook using Scheme was published in 1984 [SICP]. As Scheme became more widespread, local dialects began to diverge until students and researchers occasionally found it difficult to understand code written at other sites. Fifteen representatives of the major implementations of Scheme therefore met in October 1984 to work toward a better and more widely accepted standard for Scheme. Their report [RRRS] was published at MIT and Indiana University in the summer of 1985. Another round of revision took place in the spring of 1986 [R3RS]. The present report reflects further revisions agreed upon in a meeting that preceded the 1988 ACM Conference on Lisp and Functional Programming and in subsequent electronic mail. We intend this report to belong to the entire Scheme community, and so we grant permission to copy it in whole or in part without fee. In particular, we encourage implementors of Scheme to use this report as a starting point for manuals and other documentation, modifying it as necessary.  File: r4rs.info, Node: Acknowledgements, Prev: Background, Up: Introduction Acknowledgements ================ We would like to thank the following people for their help: Alan Bawden, Michael Blair, George Carrette, Andy Cromarty, Pavel Curtis, Jeff Dalton, Olivier Danvy, Ken Dickey, Andy Freeman, Richard Gabriel, Yekta G\"ursel, Ken Haase, Robert Hieb, Paul Hudak, Richard Kelsey, Morry Katz, Chris Lindblad, Mark Meyer, Jim Miller, Jim Philbin, John Ramsdell, Mike Shaff, Jonathan Shapiro, Julie Sussman, Perry Wagle, Daniel Weise, Henry Wu, and Ozan Yigit. We thank Carol Fessenden, Daniel Friedman, and Christopher Haynes for permission to use text from the Scheme 311 version 4 reference manual. We thank Texas Instruments, Inc. for permission to use text from the _TI Scheme Language Reference Manual._ We gladly acknowledge the influence of manuals for MIT Scheme, T, Scheme 84, Common Lisp, and Algol 60. We also thank Betty Dexter for the extreme effort she put into setting this report in TeX, and Donald Knuth for designing the program that caused her troubles. The Artificial Intelligence Laboratory of the Massachusetts Institute of Technology, the Computer Science Department of Indiana University, and the Computer and Information Sciences Department of the University of Oregon supported the preparation of this report. Support for the MIT work was provided in part by the Advanced Research Projects Agency of the Department of Defense under Office of Naval Research contract N00014-80-C-0505. Support for the Indiana University work was provided by NSF grants NCS 83-04567 and NCS 83-03325.  File: r4rs.info, Node: Overview of Scheme, Next: Lexical conventions, Prev: Introduction, Up: Top Overview of Scheme ****************** * Menu: * Semantics:: * Syntax:: * Notation and terminology::  File: r4rs.info, Node: Semantics, Next: Syntax, Prev: Overview of Scheme, Up: Overview of Scheme Semantics ========= This section gives an overview of Scheme's semantics. A detailed informal semantics is the subject of *Note Basic concepts:: through *Note Standard procedures::. For reference purposes, *Note Formal semantics:: provides a formal semantics of Scheme. Following Algol, Scheme is a statically scoped programming language. Each use of a variable is associated with a lexically apparent binding of that variable. Scheme has latent as opposed to manifest types. Types are associated with values (also called objects) rather than with variables. (Some authors refer to languages with latent types as weakly typed or dynamically typed languages.) Other languages with latent types are APL, Snobol, and other dialects of Lisp. Languages with manifest types (sometimes referred to as strongly typed or statically typed languages) include Algol 60, Pascal, and C. All objects created in the course of a Scheme computation, including procedures and continuations, have unlimited extent. No Scheme object is ever destroyed. The reason that implementations of Scheme do not (usually!) run out of storage is that they are permitted to reclaim the storage occupied by an object if they can prove that the object cannot possibly matter to any future computation. Other languages in which most objects have unlimited extent include APL and other Lisp dialects. Implementations of Scheme are required to be properly tail-recursive. This allows the execution of an iterative computation in constant space, even if the iterative computation is described by a syntactically recursive procedure. Thus with a tail-recursive implementation, iteration can be expressed using the ordinary procedure-call mechanics, so that special iteration constructs are useful only as syntactic sugar. Scheme procedures are objects in their own right. Procedures can be created dynamically, stored in data structures, returned as results of procedures, and so on. Other languages with these properties include Common Lisp and ML. One distinguishing feature of Scheme is that continuations, which in most other languages only operate behind the scenes, also have ``first-class'' status. Continuations are useful for implementing a wide variety of advanced control constructs, including non-local exits, backtracking, and coroutines. See *Note Control features::. Arguments to Scheme procedures are always passed by value, which means that the actual argument expressions are evaluated before the procedure gains control, whether the procedure needs the result of the evaluation or not. ML, C, and APL are three other languages that always pass arguments by value. This is distinct from the lazy-evaluation semantics of Haskell, or the call-by-name semantics of Algol 60, where an argument expression is not evaluated unless its value is needed by the procedure. Scheme's model of arithmetic is designed to remain as independent as possible of the particular ways in which numbers are represented within a computer. In Scheme, every integer is a rational number, every rational is a real, and every real is a complex number. Thus the distinction between integer and real arithmetic, so important to many programming languages, does not appear in Scheme. In its place is a distinction between exact arithmetic, which corresponds to the mathematical ideal, and inexact arithmetic on approximations. As in Common Lisp, exact arithmetic is not limited to integers.  File: r4rs.info, Node: Syntax, Next: Notation and terminology, Prev: Semantics, Up: Overview of Scheme Syntax ====== Scheme, like most dialects of Lisp, employs a fully parenthesized prefix notation for programs and (other) data; the grammar of Scheme generates a sublanguage of the language used for data. An important consequence of this simple, uniform representation is the susceptibility of Scheme programs and data to uniform treatment by other Scheme programs. The "read" procedure performs syntactic as well as lexical decomposition of the data it reads. The "read" procedure parses its input as data (*Note External representations::), not as program. The formal syntax of Scheme is described in *Note Formal syntax::.  File: r4rs.info, Node: Notation and terminology, Prev: Syntax, Up: Overview of Scheme Notation and terminology ======================== * Menu: * Essential and non-essential features:: * Error situations and unspecified behavior:: * Entry format:: * Evaluation examples:: * Naming conventions::  File: r4rs.info, Node: Essential and non-essential features, Next: Error situations and unspecified behavior, Prev: Notation and terminology, Up: Notation and terminology Essential and non-essential features ------------------------------------ It is required that every implementation of Scheme support features that are marked as being "essential". Features not explicitly marked as essential are not essential. Implementations are free to omit non-essential features of Scheme or to add extensions, provided the extensions are not in conflict with the language reported here. In particular, implementations must support portable code by providing a syntactic mode that preempts no lexical conventions of this report and reserves no identifiers other than those listed as syntactic keywords in *Note Identifiers::.  File: r4rs.info, Node: Error situations and unspecified behavior, Next: Entry format, Prev: Essential and non-essential features, Up: Notation and terminology Error situations and unspecified behavior ----------------------------------------- When speaking of an error situation, this report uses the phrase ``an error is signalled'' to indicate that implementations must detect and report the error. If such wording does not appear in the discussion of an error, then implementations are not required to detect or report the error, though they are encouraged to do so. An error situation that implementations are not required to detect is usually referred to simply as ``an error.'' For example, it is an error for a procedure to be passed an argument that the procedure is not explicitly specified to handle, even though such domain errors are seldom mentioned in this report. Implementations may extend a procedure's domain of definition to include such arguments. This report uses the phrase ``may report a violation of an implementation restriction'' to indicate circumstances under which an implementation is permitted to report that it is unable to continue execution of a correct program because of some restriction imposed by the implementation. Implementation restrictions are of course discouraged, but implementations are encouraged to report violations of implementation restrictions. For example, an implementation may report a violation of an implementation restriction if it does not have enough storage to run a program. If the value of an expression is said to be ``unspecified,'' then the expression must evaluate to some object without signalling an error, but the value depends on the implementation; this report explicitly does not say what value should be returned.  File: r4rs.info, Node: Entry format, Next: Evaluation examples, Prev: Error situations and unspecified behavior, Up: Notation and terminology Entry format ------------ *Note Expressions:: and *Note Standard procedures:: are organized into entries. Each entry describes one language feature or a group of related features, where a feature is either a syntactic construct or a built-in procedure. An entry begins with one or more header lines of the form -- essential CATEGORY: template if the feature is an essential feature, or simply -- CATEGORY: template if the feature is not an essential feature. If CATEGORY is ``syntax'', the entry describes an expression type, and the header line gives the syntax of the expression type. Components of expressions are designated by syntactic variables, which are written using angle brackets, for example, , . Syntactic variables should be understood to denote segments of program text; for example, stands for any string of characters which is a syntactically valid expression. The notation ... indicates zero or more occurrences of a , and ... indicates one or more occurrences of a . If CATEGORY is ``procedure'', then the entry describes a procedure, and the header line gives a template for a call to the procedure. Argument names in the template are ITALICIZED. Thus the header line -- essential procedure: vector-ref vector k indicates that the essential built-in procedure `vector-ref' takes two arguments, a vector VECTOR and an exact non-negative integer K (see below). The header lines -- essential procedure: make-vector k -- procedure: make-vector k fill indicate that in all implementations, the `make-vector' procedure must be defined to take one argument, and some implementations will extend it to take two arguments. It is an error for an operation to be presented with an argument that it is not specified to handle. For succinctness, we follow the convention that if an argument name is also the name of a type listed in *Note Disjointness of types::, then that argument must be of the named type. For example, the header line for `vector-ref' given above dictates that the first argument to `vector-ref' must be a vector. The following naming conventions also imply type restrictions: * OBJ any object * LIST, LIST1, ...LISTJ, ... list (see *Note Pairs and lists::) * Z, Z1, ...ZJ, ... complex number * X, X1, ...XJ, ... real number * Y, Y1, ...YJ, ... real number * Q, Q1, ...QJ, ... rational number * N, N1, ...NJ, ... integer * K, K1, ...KJ, ... exact non-negative integer  File: r4rs.info, Node: Evaluation examples, Next: Naming conventions, Prev: Entry format, Up: Notation and terminology Evaluation examples ------------------- The symbol ``=>'' used in program examples should be read ``evaluates to.'' For example, (* 5 8) => 40 means that the expression `(* 5 8)' evaluates to the object `40'. Or, more precisely: the expression given by the sequence of characters ```(* 5 8)''' evaluates, in the initial environment, to an object that may be represented externally by the sequence of characters ```40'''. See *Note External representations:: for a discussion of external representations of objects.  File: r4rs.info, Node: Naming conventions, Prev: Evaluation examples, Up: Notation and terminology Naming conventions ------------------ By convention, the names of procedures that always return a boolean value usually end in ```?'''. Such procedures are called predicates. By convention, the names of procedures that store values into previously allocated locations (see *Note Storage model::) usually end in ```!'''. Such procedures are called mutation procedures. By convention, the value returned by a mutation procedure is unspecified. By convention, ```->''' appears within the names of procedures that take an object of one type and return an analogous object of another type. For example, `list->vector' takes a list and returns a vector whose elements are the same as those of the list. A "thunk" is a procedure of no arguments. A "command" is a top level expression which is not a top level definition.  File: r4rs.info, Node: Lexical conventions, Next: Basic concepts, Prev: Overview of Scheme, Up: Top Lexical conventions ******************* This section gives an informal account of some of the lexical conventions used in writing Scheme programs. For a formal syntax of Scheme, see *Note Formal syntax::. Upper and lower case forms of a letter are never distinguished except within character and string constants. For example, `Foo' is the same identifier as `FOO', and `#x1AB' is the same number as `#X1ab'. * Menu: * Identifiers:: * Whitespace and comments:: * Other notations::  File: r4rs.info, Node: Identifiers, Next: Whitespace and comments, Prev: Lexical conventions, Up: Lexical conventions Identifiers =========== Most identifiers allowed by other programming languages are also acceptable to Scheme. The precise rules for forming identifiers vary among implementations of Scheme, but in all implementations a sequence of letters, digits, and ``extended alphabetic characters'' that begins with a character that cannot begin a number is an identifier. In addition, `+', `-', and `...' are identifiers. Here are some examples of identifiers: lambda q list->vector soup + V17a <=? a34kTMNs the-word-recursion-has-many-meanings Extended alphabetic characters may be used within identifiers as if they were letters. The following are extended alphabetic characters: + - . * / < = > ! ? : $ % _ & ~ ^ See *Note Lexical structure:: for a formal syntax of identifiers. Identifiers have several uses within Scheme programs: * Certain identifiers are reserved for use as syntactic keywords (see below). * Any identifier that is not a syntactic keyword may be used as a variable (see *Note Variables and regions::). * When an identifier appears as a literal or within a literal (see *Note Literal expressions::), it is being used to denote a _symbol_ (see *Note Symbols::). The following identifiers are syntactic keywords, and should not be used as variables: => do or and else quasiquote begin if quote case lambda set! cond let unquote define let* unquote-splicing delay letrec Some implementations allow all identifiers, including syntactic keywords, to be used as variables. This is a compatible extension to the language, but ambiguities in the language result when the restriction is relaxed, and the ways in which these ambiguities are resolved vary between implementations.  File: r4rs.info, Node: Whitespace and comments, Next: Other notations, Prev: Identifiers, Up: Lexical conventions Whitespace and comments ======================= "Whitespace" characters are spaces and newlines. (Implementations typically provide additional whitespace characters such as tab or page break.) Whitespace is used for improved readability and as necessary to separate tokens from each other, a token being an indivisible lexical unit such as an identifier or number, but is otherwise insignificant. Whitespace may occur between any two tokens, but not within a token. Whitespace may also occur inside a string, where it is significant. A semicolon (`;') indicates the start of a comment. The comment continues to the end of the line on which the semicolon appears. Comments are invisible to Scheme, but the end of the line is visible as whitespace. This prevents a comment from appearing in the middle of an identifier or number. ;;; The FACT procedure computes the factorial ;;; of a non-negative integer. (define fact (lambda (n) (if (= n 0) 1 ;Base case: return 1 (* n (fact (- n 1))))))  File: r4rs.info, Node: Other notations, Prev: Whitespace and comments, Up: Lexical conventions Other notations =============== For a description of the notations used for numbers, see *Note Numbers::. . + - These are used in numbers, and may also occur anywhere in an identifier except as the first character. A delimited plus or minus sign by itself is also an identifier. A delimited period (not occurring within a number or identifier) is used in the notation for pairs (*Note Pairs and lists::), and to indicate a rest-parameter in a formal parameter list (*Note Lambda expressions::). A delimited sequence of three successive periods is also an identifier. ( ) Parentheses are used for grouping and to notate lists (*Note Pairs and lists::). ' The single quote character is used to indicate literal data (*Note Literal expressions::). ` The backquote character is used to indicate almost-constant data (*Note Quasiquotation::). , ,@ The character comma and the sequence comma at-sign are used in conjunction with backquote (*Note Quasiquotation::). " The double quote character is used to delimit strings (*Note Strings::). \ Backslash is used in the syntax for character constants (*Note Characters::) and as an escape character within string constants (*Note Strings::). [ ] { } Left and right square brackets and curly braces are reserved for possible future extensions to the language. # Sharp sign is used for a variety of purposes depending on the character that immediately follows it: #t #f These are the boolean constants (*Note Booleans::). #\ This introduces a character constant (*Note Characters::). #( This introduces a vector constant (*Note Vectors::). Vector constants are terminated by `)' . #e #i #b #o #d #x These are used in the notation for numbers (*Note Syntax of numerical constants::).  File: r4rs.info, Node: Basic concepts, Next: Expressions, Prev: Lexical conventions, Up: Top Basic concepts ************** * Menu: * Variables and regions:: * True and false:: * External representations:: * Disjointness of types:: * Storage model::  File: r4rs.info, Node: Variables and regions, Next: True and false, Prev: Basic concepts, Up: Basic concepts Variables and regions ===================== Any identifier that is not a syntactic keyword (see *Note Identifiers::) may be used as a variable. A variable may name a location where a value can be stored. A variable that does so is said to be _bound_ to the location. The set of all visible bindings in effect at some point in a program is known as the _environment_ in effect at that point. The value stored in the location to which a variable is bound is called the variable's value. By abuse of terminology, the variable is sometimes said to name the value or to be bound to the value. This is not quite accurate, but confusion rarely results from this practice. Certain expression types are used to create new locations and to bind variables to those locations. The most fundamental of these _binding constructs_ is the lambda expression, because all other binding constructs can be explained in terms of lambda expressions. The other binding constructs are `let', `let*', `letrec', and `do' expressions (see *Note Lambda expressions::, *Note Binding constructs::, and *Note Iteration::). Like Algol and Pascal, and unlike most other dialects of Lisp except for Common Lisp, Scheme is a statically scoped language with block structure. To each place where a variable is bound in a program there corresponds a "region" of the program text within which the binding is effective. The region is determined by the particular binding construct that establishes the binding; if the binding is established by a lambda expression, for example, then its region is the entire lambda expression. Every reference to or assignment of a variable refers to the binding of the variable that established the innermost of the regions containing the use. If there is no binding of the variable whose region contains the use, then the use refers to the binding for the variable in the top level environment, if any (section *Note Standard procedures::); if there is no binding for the identifier, it is said to be "unbound".  File: r4rs.info, Node: True and false, Next: External representations, Prev: Variables and regions, Up: Basic concepts True and false ============== Any Scheme value can be used as a boolean value for the purpose of a conditional test. As explained in *Note Booleans::, all values count as true in such a test except for `#f'. This report uses the word ``true'' to refer to any Scheme value that counts as true, and the word ``false'' to refer to `#f'. _Note:_ In some implementations the empty list also counts as false instead of true.  File: r4rs.info, Node: External representations, Next: Disjointness of types, Prev: True and false, Up: Basic concepts External representations ======================== An important concept in Scheme (and Lisp) is that of the _external representation_ of an object as a sequence of characters. For example, an external representation of the integer 28 is the sequence of characters ```28''', and an external representation of a list consisting of the integers 8 and 13 is the sequence of characters ```(8 13)'''. The external representation of an object is not necessarily unique. The integer 28 also has representations ```#e28.000''' and ```#x1c''', and the list in the previous paragraph also has the representations ```( 08 13 )''' and ```(8 . (13 . ()))''' (see *Note Pairs and lists::). Many objects have standard external representations, but some, such as procedures, do not have standard representations (although particular implementations may define representations for them). An external representation may be written in a program to obtain the corresponding object (see `quote', *Note Literal expressions::). External representations can also be used for input and output. The procedure `read' (*Note Input::) parses external representations, and the procedure `Output' (*Note Output::) generates them. Together, they provide an elegant and powerful input/output facility. Note that the sequence of characters ```(+ 2 6)''' is _not_ an external representation of the integer 8, even though it _is_ an expression evaluating to the integer 8; rather, it is an external representation of a three-element list, the elements of which are the symbol `+' and the integers 2 and 6. Scheme's syntax has the property that any sequence of characters that is an expression is also the external representation of some object. This can lead to confusion, since it may not be obvious out of context whether a given sequence of characters is intended to denote data or program, but it is also a source of power, since it facilitates writing programs such as interpreters and compilers that treat programs as data (or vice versa). The syntax of external representations of various kinds of objects accompanies the description of the primitives for manipulating the objects in the appropriate sections of *Note Standard procedures::.  File: r4rs.info, Node: Disjointness of types, Next: Storage model, Prev: External representations, Up: Basic concepts Disjointness of types ===================== No object satisfies more than one of the following predicates: boolean? pair? symbol? number? char? string? vector? procedure? These predicates define the types _boolean_, _pair_, _symbol_, _number_, _char_ (or _character_), _string_, _vector_, and _procedure_.  File: r4rs.info, Node: Storage model, Prev: Disjointness of types, Up: Basic concepts Storage model ============= Variables and objects such as pairs, vectors, and strings implicitly denote locations or sequences of locations. A string, for example, denotes as many locations as there are characters in the string. (These locations need not correspond to a full machine word.) A new value may be stored into one of these locations using the `string-set!' procedure, but the string continues to denote the same locations as before. An object fetched from a location, by a variable reference or by a procedure such as `car', `vector-ref', or `string-ref', is equivalent in the sense of `eqv?' (section *Note Equivalence predicates::) to the object last stored in the location before the fetch. Every location is marked to show whether it is in use. No variable or object ever refers to a location that is not in use. Whenever this report speaks of storage being allocated for a variable or object, what is meant is that an appropriate number of locations are chosen from the set of locations that are not in use, and the chosen locations are marked to indicate that they are now in use before the variable or object is made to denote them. In many systems it is desirable for constants (i.e. the values of literal expressions) to reside in read-only-memory. To express this, it is convenient to imagine that every object that denotes locations is associated with a flag telling whether that object is mutable or immutable. The constants and the strings returned by `symbol->string' are then the immutable objects, while all objects created by the other procedures listed in this report are mutable. It is an error to attempt to store a new value into a location that is denoted by an immutable object.  File: r4rs.info, Node: Expressions, Next: Program structure, Prev: Basic concepts, Up: Top Expressions *********** A Scheme expression is a construct that returns a value, such as a variable reference, literal, procedure call, or conditional. Expression types are categorized as _primitive_ or _derived_. Primitive expression types include variables and procedure calls. Derived expression types are not semantically primitive, but can instead be explained in terms of the primitive constructs as in *Note Formal derived expression types::. They are redundant in the strict sense of the word, but they capture common patterns of usage, and are therefore provided as convenient abbreviations. * Menu: * Primitive expression types:: * Derived expression types::  File: r4rs.info, Node: Primitive expression types, Next: Derived expression types, Prev: Expressions, Up: Expressions Primitive expression types ========================== * Menu: * Variable references:: * Literal expressions:: * Procedure calls:: * Lambda expressions:: * Conditionals:Primitive Conditionals * Assignments::  File: r4rs.info, Node: Variable references, Next: Literal expressions, Prev: Primitive expression types, Up: Primitive expression types Variable references ------------------- -- essential syntax: An expression consisting of a variable (*Note Variables and regions::) is a variable reference. The value of the variable reference is the value stored in the location to which the variable is bound. It is an error to reference an unbound variable. (define x 28) x => 28  File: r4rs.info, Node: Literal expressions, Next: Procedure calls, Prev: Variable references, Up: Primitive expression types Literal expressions ------------------- -- essential syntax: quote -- essential syntax: ' -- essential syntax: `(quote )' evaluates to . may be any external representation of a Scheme object (see *Note External representations::). This notation is used to include literal constants in Scheme code. (quote a) => a (quote #(a b c)) => #(a b c) (quote (+ 1 2)) => (+ 1 2) `(quote )' may be abbreviated as '. The two notations are equivalent in all respects. 'a => a '#(a b c) => #(a b c) '() => () '(+ 1 2) => (+ 1 2) '(quote a) => (quote a) ''a => (quote a) Numerical constants, string constants, character constants, and boolean constants evaluate ``to themselves''; they need not be quoted. '"abc" => "abc" "abc" => "abc" '145932 => 145932 145932 => 145932 '#t => #t #t => #t As noted in *Note Storage model::, it is an error to alter a constant (i.e. the value of a literal expression) using a mutation procedure like `set-car!' or `string-set!'.  File: r4rs.info, Node: Procedure calls, Next: Lambda expressions, Prev: Literal expressions, Up: Primitive expression types Procedure calls --------------- -- essential syntax: ... A procedure call is written by simply enclosing in parentheses expressions for the procedure to be called and the arguments to be passed to it. The operator and operand expressions are evaluated (in an unspecified order) and the resulting procedure is passed the resulting arguments. (+ 3 4) => 7 ((if #f + *) 3 4) => 12 A number of procedures are available as the values of variables in the initial environment; for example, the addition and multiplication procedures in the above examples are the values of the variables `+' and `*'. New procedures are created by evaluating lambda expressions (see section *Note Lambda expressions::). Procedure calls are also called _combinations_. _Note:_ In contrast to other dialects of Lisp, the order of evaluation is unspecified, and the operator expression and the operand expressions are always evaluated with the same evaluation rules. _Note:_ Although the order of evaluation is otherwise unspecified, the effect of any concurrent evaluation of the operator and operand expressions is constrained to be consistent with some sequential order of evaluation. The order of evaluation may be chosen differently for each procedure call. _Note:_ In many dialects of Lisp, the empty combination, `()', is a legitimate expression. In Scheme, combinations must have at least one subexpression, so `()' is not a syntactically valid expression.  File: r4rs.info, Node: Lambda expressions, Next: Primitive Conditionals, Prev: Procedure calls, Up: Primitive expression types Lambda expressions ------------------ -- essential syntax: lambda _Syntax:_ should be a formal arguments list as described below, and should be a sequence of one or more expressions. _Semantics:_ A lambda expression evaluates to a procedure. The environment in effect when the lambda expression was evaluated is remembered as part of the procedure. When the procedure is later called with some actual arguments, the environment in which the lambda expression was evaluated will be extended by binding the variables in the formal argument list to fresh locations, the corresponding actual argument values will be stored in those locations, and the expressions in the body of the lambda expression will be evaluated sequentially in the extended environment. The result of the last expression in the body will be returned as the result of the procedure call. (lambda (x) (+ x x)) => _a procedure_ ((lambda (x) (+ x x)) 4) => 8 (define reverse-subtract (lambda (x y) (- y x))) (reverse-subtract 7 10) => 3 (define add4 (let ((x 4)) (lambda (y) (+ x y)))) (add4 6) => 10 should have one of the following forms: * `( ...)': The procedure takes a fixed number of arguments; when the procedure is called, the arguments will be stored in the bindings of the corresponding variables. * : The procedure takes any number of arguments; when the procedure is called, the sequence of actual arguments is converted into a newly allocated list, and the list is stored in the binding of the . * `( ... . )': If a space-delimited period precedes the last variable, then the value stored in the binding of the last variable will be a newly allocated list of the actual arguments left over after all the other actual arguments have been matched up against the other formal arguments. It is an error for a to appear more than once in . ((lambda x x) 3 4 5 6) => (3 4 5 6) ((lambda (x y . z) z) 3 4 5 6) => (5 6) Each procedure created as the result of evaluating a lambda expression is tagged with a storage location, in order to make `eqv?' and `eq?' work on procedures (see *Note Equivalence predicates::).  File: r4rs.info, Node: Primitive Conditionals, Next: Assignments, Prev: Lambda expressions, Up: Primitive expression types Conditionals ------------ -- essential syntax: if -- syntax: if _Syntax:_ , , and may be arbitrary expressions. _Semantics:_ An `if' expression is evaluated as follows: first, is evaluated. If it yields a true value (see *Note Booleans::), then is evaluated and its value is returned. Otherwise is evaluated and its value is returned. If yields a false value and no is specified, then the result of the expression is unspecified. (if (> 3 2) 'yes 'no) => yes (if (> 2 3) 'yes 'no) => no (if (> 3 2) (- 3 2) (+ 3 2)) => 1  File: r4rs.info, Node: Assignments, Prev: Primitive Conditionals, Up: Primitive expression types Assignments ----------- -- essential syntax: set! is evaluated, and the resulting value is stored in the location to which is bound. must be bound either in some region enclosing the `set!' expression or at top level. The result of the `set!' expression is unspecified. (define x 2) (+ x 1) => 3 (set! x 4) => _unspecified_ (+ x 1) => 5  File: r4rs.info, Node: Derived expression types, Prev: Primitive expression types, Up: Expressions Derived expression types ======================== For reference purposes, *Note Formal derived expression types:: gives rewrite rules that will convert constructs described in this section into the primitive constructs described in the previous section. * Menu: * Conditionals:: * Binding constructs:: * Sequencing:: * Iteration:: * Delayed evaluation:: * Quasiquotation::  File: r4rs.info, Node: Conditionals, Next: Binding constructs, Prev: Derived expression types, Up: Derived expression types Conditionals ------------ -- essential syntax: cond ... _Syntax:_ Each should be of the form ( ...) where is any expression. The last may be an ``else clause,'' which has the form (else ...). _Semantics:_ A `cond' expression is evaluated by evaluating the expressions of successive s in order until one of them evaluates to a true value (see *Note Booleans::). When a evaluates to a true value, then the remaining s in its are evaluated in order, and the result of the last in the is returned as the result of the entire `cond' expression. If the selected contains only the and no s, then the value of the is returned as the result. If all s evaluate to false values, and there is no else clause, then the result of the conditional expression is unspecified; if there is an else clause, then its s are evaluated, and the value of the last one is returned. (cond ((> 3 2) 'greater) ((< 3 2) 'less)) => greater (cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) => equal Some implementations support an alternative syntax, `( => )', where is an expression. If evaluates to a true value, then is evaluated. Its value must be a procedure of one argument; this procedure is then invoked on the value of the . (cond ((assv 'b '((a 1) (b 2))) => cadr) (else `#f')) => 2 -- essential syntax: case ... _Syntax:_ may be any expression. Each should have the form (( ...) ...), where each is an external representation of some object. All the s must be distinct. The last may be an ``else clause,'' which has the form (else ...). _Semantics:_ A `case' expression is evaluated as follows. is evaluated and its result is compared against each . If the result of evaluating is equivalent (in the sense of `eqv?'; see *Note Equivalence predicates::) to a , then the expressions in the corresponding are evaluated from left to right and the result of the last expression in the is returned as the result of the `case' expression. If the result of evaluating is different from every , then if there is an else clause its expressions are evaluated and the result of the last is the result of the `case' expression; otherwise the result of the `case' expression is unspecified. (case (* 2 3) ((2 3 5 7) 'prime) ((1 4 6 8 9) 'composite)) => composite (case (car '(c d)) ((a) 'a) ((b) 'b)) => _unspecified_ (case (car '(c d)) ((a e i o u) 'vowel) ((w y) 'semivowel) (else 'consonant)) => consonant -- essential syntax: and ... The expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see *Note Booleans::) is returned. Any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then `#t' is returned. (and (= 2 2) (> 2 1)) => #t (and (= 2 2) (< 2 1)) => #f (and 1 2 'c '(f g)) => (f g) (and) => #t -- essential syntax: or ... The expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see *Note Booleans::) is returned. Any remaining expressions are not evaluated. If all expressions evaluate to false values, the value of the last expression is returned. If there are no expressions then `#f' is returned. (or (= 2 2) (> 2 1)) => #t (or (= 2 2) (< 2 1)) => #t (or #f #f #f) => #f (or (memq 'b '(a b c)) (/ 3 0)) => (b c)