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: Binding constructs, Next: Sequencing, Prev: Conditionals, 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. -- essential 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 of the last expression of is 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', *Note Iteration::. -- 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 -- essential 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 of the last expression in is 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: r4rs.info, Node: Sequencing, Next: Iteration, Prev: Binding constructs, Up: Derived expression types Sequencing ---------- -- essential syntax: begin ... The s are evaluated sequentially from left to right, and the value of the last is 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 _Note:_ [SICP] uses the keyword `sequence' instead of `begin'.  File: r4rs.info, Node: Iteration, Next: Delayed evaluation, Prev: Sequencing, Up: Derived expression types Iteration --------- -- syntax: do _Syntax:_ should have the form (( ) ...), should be of the form ( ...), and should be a sequence of one or more expressions. `Do' is an iteration construct. It specifies a set of variables to be bound, how they are to be initialized at the start, and how they are to be updated on each iteration. When a termination condition is met, the loop exits with a specified result value. `Do' expressions are evaluated as follows: The 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 *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 of the last is returned as the value of the `do' expression. 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 -- syntax: let Some implementations of Scheme permit a variant on the syntax of `let' called ``named `let''' which provides a more general looping construct than `do', and may also be used to express recursions. Named `let' has the same syntax and semantics as ordinary `let' except that 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: r4rs.info, Node: Delayed evaluation, Next: Quasiquotation, Prev: Iteration, Up: Derived expression types Delayed evaluation ------------------ -- 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. See the description of `force' (*Note Control features::) for a more complete description of `delay'.  File: r4rs.info, Node: Quasiquotation, Prev: Delayed evaluation, Up: Derived expression types Quasiquotation -------------- -- essential syntax: quasiquote