This is r5rs.info, produced by makeinfo version 4.2 from r5rs.texi. INFO-DIR-SECTION The Algorithmic Language Scheme START-INFO-DIR-ENTRY * R5RS: (r5rs). The Revised(5) Report on Scheme. END-INFO-DIR-ENTRY 20 February 1998  File: r5rs.info, Node: Conditional, Next: Binding constructs, Prev: Derived expression types, Up: Derived expression types Conditionals ------------ - library syntax: cond ..., _Syntax:_ Each should be of the form ( ...,) where is any expression. Alternatively, a may be of the form ( => ) 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 section *note Booleans::). When a evaluates to a true value, then the remaining s in its are evaluated in order, and the result(s) of the last in the is(are) returned as the result(s) 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 the selected uses the `=>' alternate form, then the is evaluated. Its value must be a procedure that accepts one argument; this procedure is then called on the value of the and the value(s) returned by this procedure is(are) returned by the `cond' expression. 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(s) of the last one is(are) returned. (cond ((> 3 2) 'greater) ((< 3 2) 'less)) ==> greater (cond ((> 3 3) 'greater) ((< 3 3) 'less) (else 'equal)) ==> equal (cond ((assv 'b '((a 1) (b 2))) => cadr) (else #f)) ==> 2 - library 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 section *note Equivalence predicates::) to a , then the expressions in the corresponding are evaluated from left to right and the result(s) of the last expression in the is(are) returned as the result(s) 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(s) of the last is(are) the result(s) 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 - library syntax: and ..., The expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value (see section *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 - library syntax: or ..., The expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value (see section *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)  File: r5rs.info, Node: Binding constructs, Next: Sequencing, Prev: Conditional, Up: Derived expression types Binding constructs ------------------ The three binding constructs `let', `let*', and `letrec' give Scheme a block structure, like Algol 60. The syntax of the three constructs is identical, but they differ in the regions they establish for their variable bindings. In a `let' expression, the initial values are computed before any of the variables become bound; in a `let*' expression, the bindings and evaluations are performed sequentially; while in a `letrec' expression, all the bindings are in effect while their initial values are being computed, thus allowing mutually recursive definitions. - library syntax: let _Syntax:_ should have the form (( ) ...,), where each is an expression, and should be a sequence of one or more expressions. It is an error for a to appear more than once in the list of variables being bound. _Semantics:_ The s are evaluated in the current environment (in some unspecified order), the s are bound to fresh locations holding the results, the is evaluated in the extended environment, and the value(s) of the last expression of is(are) returned. Each binding of a has as its region. (let ((x 2) (y 3)) (* x y)) ==> 6 (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x))) ==> 35 See also named `let', section *Note Iteration::. - library syntax: let* _Syntax:_ should have the form (( ) ...,), and should be a sequence of one or more expressions. _Semantics:_ `Let*' is similar to `let', but the bindings are performed sequentially from left to right, and the region of a binding indicated by `( )' is that part of the `let*' expression to the right of the binding. Thus the second binding is done in an environment in which the first binding is visible, and so on. (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x))) ==> 70 - library syntax: letrec _Syntax:_ should have the form (( ) ...,), and should be a sequence of one or more expressions. It is an error for a to appear more than once in the list of variables being bound. _Semantics:_ The s are bound to fresh locations holding undefined values, the s are evaluated in the resulting environment (in some unspecified order), each is assigned to the result of the corresponding , the is evaluated in the resulting environment, and the value(s) of the last expression in is(are) returned. Each binding of a has the entire `letrec' expression as its region, making it possible to define mutually recursive procedures. (letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88)) ==> #t One restriction on `letrec' is very important: it must be possible to evaluate each without assigning or referring to the value of any . If this restriction is violated, then it is an error. The restriction is necessary because Scheme passes arguments by value rather than by name. In the most common uses of `letrec', all the s are lambda expressions and the restriction is satisfied automatically.  File: r5rs.info, Node: Sequencing, Next: Iteration, Prev: Binding constructs, Up: Derived expression types Sequencing ---------- - library syntax: begin ..., The s are evaluated sequentially from left to right, and the value(s) of the last is(are) returned. This expression type is used to sequence side effects such as input and output. (define x 0) (begin (set! x 5) (+ x 1)) ==> 6 (begin (display "4 plus 1 equals ") (display (+ 4 1))) ==> _unspecified_ _and prints_ 4 plus 1 equals 5  File: r5rs.info, Node: Iteration, Next: Delayed evaluation, Prev: Sequencing, Up: Derived expression types Iteration --------- - library syntax: do (( ) ...) ( ...) ... `Do' is an iteration construct. It specifies a set of variables to be bound, how they are to be initialized at the start, and how they are to be updated on each iteration. When a termination condition is met, the loop exits after evaluating the s. `Do' expressions are evaluated as follows: The expressions are evaluated (in some unspecified order), the s are bound to fresh locations, the results of the expressions are stored in the bindings of the s, and then the iteration phase begins. Each iteration begins by evaluating ; if the result is false (see section *note Booleans::), then the expressions are evaluated in order for effect, the expressions are evaluated in some unspecified order, the s are bound to fresh locations, the results of the s are stored in the bindings of the s, and the next iteration begins. If evaluates to a true value, then the s are evaluated from left to right and the value(s) of the last is(are) returned. If no s are present, then the value of the `do' expression is unspecified. The region of the binding of a consists of the entire `do' expression except for the s. It is an error for a to appear more than once in the list of `do' variables. A may be omitted, in which case the effect is the same as if `( )' had been written instead of `( )'. (do ((vec (make-vector 5)) (i 0 (+ i 1))) ((= i 5) vec) (vector-set! vec i i)) ==> #(0 1 2 3 4) (let ((x '(1 3 5 7 9))) (do ((x x (cdr x)) (sum 0 (+ sum (car x)))) ((null? x) sum))) ==> 25 - library syntax: let "Named `let'" is a variant on the syntax of `let' which provides a more general looping construct than `do' and may also be used to express recursions. It has the same syntax and semantics as ordinary `let' except that is bound within to a procedure whose formal arguments are the bound variables and whose body is . Thus the execution of may be repeated by invoking the procedure named by . (let loop ((numbers '(3 -2 1 6 -5)) (nonneg '()) (neg '())) (cond ((null? numbers) (list nonneg neg)) ((>= (car numbers) 0) (loop (cdr numbers) (cons (car numbers) nonneg) neg)) ((< (car numbers) 0) (loop (cdr numbers) nonneg (cons (car numbers) neg))))) ==> ((6 1 3) (-5 -2))  File: r5rs.info, Node: Delayed evaluation, Next: Quasiquotation, Prev: Iteration, Up: Derived expression types Delayed evaluation ------------------ - library syntax: delay The `delay' construct is used together with the procedure `force' to implement "lazy evaluation" or "call by need". (delay ) returns an object called a "promise" which at some point in the future may be asked (by the `force' procedure) to evaluate , and deliver the resulting value. The effect of returning multiple values is unspecified. See the description of `force' (section *note Control features::) for a more complete description of `delay'.  File: r5rs.info, Node: Quasiquotation, Prev: Delayed evaluation, Up: Derived expression types Quasiquotation -------------- - syntax: quasiquote - syntax: ` "Backquote" or "quasiquote" expressions are useful for constructing a list or vector structure when most but not all of the desired structure is known in advance. If no commas appear within the , the result of evaluating ` is equivalent to the result of evaluating '. If a comma appears within the , however, the expression following the comma is evaluated ("unquoted") and its result is inserted into the structure instead of the comma and the expression. If a comma appears followed immediately by an at-sign (@), then the following expression must evaluate to a list; the opening and closing parentheses of the list are then "stripped away" and the elements of the list are inserted in place of the comma at-sign expression sequence. A comma at-sign should only appear within a list or vector . `(list ,(+ 1 2) 4) ==> (list 3 4) (let ((name 'a)) `(list ,name ',name)) ==> (list a (quote a)) `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b) ==> (a 3 4 5 6 b) `((`foo' ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons))) ==> ((foo 7) . cons) `#(10 5 ,(sqrt 4) ,@(map sqrt '(16 9)) 8) ==> #(10 5 2 4 3 8) Quasiquote forms may be nested. Substitutions are made only for unquoted components appearing at the same nesting level as the outermost backquote. The nesting level increases by one inside each successive quasiquotation, and decreases by one inside each unquotation. `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) ==> (a `(b ,(+ 1 2) ,(foo 4 d) e) f) (let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e)) ==> (a `(b ,x ,'y d) e) The two notations ` and (quasiquote ) are identical in all respects. `,' is identical to `(unquote )', and `,@' is identical to `(unquote-splicing )'. The external syntax generated by `write' for two-element lists whose car is one of these symbols may vary between implementations. (quasiquote (list (unquote (+ 1 2)) 4)) ==> (list 3 4) '(quasiquote (list (unquote (+ 1 2)) 4)) ==> `(list ,(+ 1 2) 4) __i.e., (quasiquote (list (unquote (+ 1 2)) 4)) Unpredictable behavior can result if any of the symbols `quasiquote', `unquote', or `unquote-splicing' appear in positions within a otherwise than as described above.  File: r5rs.info, Node: Macros, Prev: Derived expression types, Up: Expressions Macros ====== * Menu: * Binding constructs for syntactic keywords:: * Pattern language:: Scheme programs can define and use new derived expression types, called _macros_. Program-defined expression types have the syntax ( ...) where is an identifier that uniquely determines the expression type. This identifier is called the _syntactic keyword_, or simply _keyword_, of the macro. The number of the s, and their syntax, depends on the expression type. Each instance of a macro is called a _use_ of the macro. The set of rules that specifies how a use of a macro is transcribed into a more primitive expression is called the _transformer_ of the macro. The macro definition facility consists of two parts: * A set of expressions used to establish that certain identifiers are macro keywords, associate them with macro transformers, and control the scope within which a macro is defined, and * a pattern language for specifying macro transformers. The syntactic keyword of a macro may shadow variable bindings, and local variable bindings may shadow keyword bindings. All macros defined using the pattern language are "hygienic" and "referentially transparent" and thus preserve Scheme's lexical scoping [Kohlbecker86], [ hygienic], [Bawden88], [macrosthatwork], [syntacticabstraction]: * If a macro transformer inserts a binding for an identifier (variable or keyword), the identifier will in effect be renamed throughout its scope to avoid conflicts with other identifiers. Note that a `define' at top level may or may not introduce a binding; see section *Note Definitions::. * If a macro transformer inserts a free reference to an identifier, the reference refers to the binding that was visible where the transformer was specified, regardless of any local bindings that may surround the use of the macro.  File: r5rs.info, Node: Binding constructs for syntactic keywords, Next: Pattern language, Prev: Macros, Up: Macros Binding constructs for syntactic keywords ----------------------------------------- `Let-syntax' and `letrec-syntax' are analogous to `let' and `letrec', but they bind syntactic keywords to macro transformers instead of binding variables to locations that contain values. Syntactic keywords may also be bound at top level; see section *Note Syntax definitions::. - syntax: let-syntax _Syntax:_ should have the form (( ) ...,) Each is an identifier, each is an instance of `syntax-rules', and should be a sequence of one or more expressions. It is an error for a to appear more than once in the list of keywords being bound. _Semantics:_ The is expanded in the syntactic environment obtained by extending the syntactic environment of the `let-syntax' expression with macros whose keywords are the s, bound to the specified transformers. Each binding of a has as its region. (let-syntax ((when (syntax-rules () ((when test stmt1 stmt2 ...) (if test (begin stmt1 stmt2 ...)))))) (let ((if #t)) (when if (set! if 'now)) if)) ==> now (let ((x 'outer)) (let-syntax ((m (syntax-rules () ((m) x)))) (let ((x 'inner)) (m)))) ==> outer - syntax: letrec-syntax _Syntax:_ Same as for `let-syntax'. _Semantics:_ The is expanded in the syntactic environment obtained by extending the syntactic environment of the `letrec-syntax' expression with macros whose keywords are the s, bound to the specified transformers. Each binding of a has the as well as the within its region, so the transformers can transcribe expressions into uses of the macros introduced by the `letrec-syntax' expression. (letrec-syntax ((my-or (syntax-rules () ((my-or) #f) ((my-or e) e) ((my-or e1 e2 ...) (let ((temp e1)) (if temp temp (my-or e2 ...))))))) (let ((x #f) (y 7) (temp 8) (let odd?) (if even?)) (my-or x (let temp) (if y) y))) ==> 7  File: r5rs.info, Node: Pattern language, Prev: Binding constructs for syntactic keywords, Up: Macros Pattern language ---------------- A has the following form: - : syntax-rules ..., _Syntax:_ is a list of identifiers and each should be of the form (