iteration.texinfo (3457B)
1 @node Iteration 2 @subsection Iteration 3 4 @c Since @deffn would put this whole complex form on one line in most 5 @c output types, I've put the actual syntax after definition header. 6 @c Maybe there's a better way? 7 @deffn syntax do 8 9 @format 10 @code{(do ((}@svar{variable@sub{1}} @svar{init@sub{1}} @svar{step@sub{1}}@code{)} 11 @dots{}@code{)} 12 @code{(}@svar{test} @svar{expression} @dots{}@code{)} 13 @svar{command} @dots{}@code{)} 14 @end format 15 16 Syntax: All of @svar{init}, @svar{step}, @svar{test}, and @svar{command} are expressions. 17 18 Semantics: 19 A @code{do} expression is an iteration construct. It specifies a set of variables to 20 be bound, how they are to be initialized at the start, and how they are 21 to be updated on each iteration. When a termination condition is met, 22 the loop exits after evaluating the @svar{expression}s. 23 24 A @code{do} expression is evaluated as follows: 25 The @svar{init} expressions are evaluated (in some unspecified order), 26 the @svar{variable}s are bound to fresh locations, the results of the 27 @svar{init} expressions are stored in the bindings of the 28 @svar{variable}s, and then the iteration phase begins. 29 30 Each iteration begins by evaluating @svar{test}; if the result is 31 false (@xref{Booleans}), then the @svar{command} 32 expressions are evaluated in order for effect, the @svar{step} 33 expressions are evaluated in some unspecified order, the 34 @svar{variable}s are bound to fresh locations, the results of the 35 @svar{step}s are stored in the bindings of the 36 @svar{variable}s, and the next iteration begins. 37 38 If @svar{test} evaluates to a true value, then the 39 @svar{expression}s are evaluated from left to right and the values of 40 the last @svar{expression} are returned. If no @svar{expression}s 41 are present, then the value of the @code{do} expression is unspecified. 42 43 The region of the binding of a @svar{variable} 44 consists of the entire @code{do} expression except for the @svar{init}s. 45 It is an error for a @svar{variable} to appear more than once in the 46 list of @code{do} variables. 47 48 A @svar{step} can be omitted, in which case the effect is the 49 same as if @code{(}@svar{variable} @svar{init} @svar{variable}@code{)} had 50 been written instead of @code{(}@svar{variable} @svar{init}@code{)}. 51 52 @lisp 53 (do ((vec (make-vector 5)) 54 (i 0 (+ i 1))) 55 ((= i 5) vec) 56 (vector-set! vec i i)) @result{} #(0 1 2 3 4) 57 58 (let ((x '(1 3 5 7 9))) 59 (do ((x x (cdr x)) 60 (sum 0 (+ sum (car x)))) 61 ((null? x) sum))) @result{} 25 62 @end lisp 63 64 @end deffn 65 66 @deffn syntax let @svar{variable} @svar{bindings} @svar{body} 67 68 ``Named @code{let}'' is a variant on the syntax of @code{let} which provides 69 a more general looping construct than @code{do} and can also be used to express 70 recursion. 71 It has the same syntax and semantics as ordinary @code{let} 72 except that @svar{variable} is bound within @svar{body} to a procedure 73 whose formal arguments are the bound variables and whose body is 74 @svar{body}. Thus the execution of @svar{body} can be repeated by 75 invoking the procedure named by @svar{variable}. 76 77 @lisp 78 (let loop ((numbers '(3 -2 1 6 -5)) 79 (nonneg '()) 80 (neg '())) 81 (cond ((null? numbers) (list nonneg neg)) 82 ((>= (car numbers) 0) 83 (loop (cdr numbers) 84 (cons (car numbers) nonneg) 85 neg)) 86 ((< (car numbers) 0) 87 (loop (cdr numbers) 88 nonneg 89 (cons (car numbers) neg))))) 90 @result{} ((6 1 3) (-5 -2)) 91 @end lisp 92 93 @end deffn