* scheme-control.texi (Multiple Values): Documented concept of
[bpt/guile.git] / doc / scheme-control.texi
1 @page
2 @node Control Mechanisms
3 @chapter Controlling the Flow of Program Execution
4
5 @menu
6 * begin:: Evaluating a sequence of expressions.
7 * if cond case:: Simple conditional evaluation.
8 * and or:: Conditional evaluation of a sequence.
9 * while do:: Iteration mechanisms.
10 * Continuations:: Continuations.
11 * Multiple Values:: Returning and accepting multiple values.
12 * Exceptions:: Throwing and catching exceptions.
13 * Error Reporting:: Procedures for signaling errors.
14 * Dynamic Wind:: Guarding against non-local entrance/exit.
15 @end menu
16
17
18 @node begin
19 @section Evaluating a Sequence of Expressions
20
21 @c FIXME::martin: Review me!
22
23 @c FIXME::martin: Maybe add examples?
24
25 @cindex begin
26 @cindex sequencing
27 @cindex expression sequencing
28
29 @code{begin} is used for grouping several expression together so that
30 they syntactically are treated as if they were one expression. This is
31 particularly important when syntactic expressions are used which only
32 allow one expression, but the programmer wants to use more than one
33 expression in that place. As an example, consider the conditional
34 expression below:
35
36 @lisp
37 (if (> x 0)
38 (begin (display "greater") (newline)))
39 @end lisp
40
41 If the two calls to @code{display} and @code{newline} were not embedded
42 in a @code{begin}--statement, the call to @code{newline} would get
43 misinterpreted as the else--branch of the @code{if}--expression.
44
45 @deffn syntax begin expr1 expr2 @dots{}
46 The expression(s) are evaluated in left--to--right order and the value
47 of the last expression is returned as the value of the
48 @code{begin}--expression. This expression type is used when the
49 expressions before the last one are evaluated for their side effects.
50 @end deffn
51
52 @node if cond case
53 @section Simple Conditional Evaluation
54
55 @c FIXME::martin: Review me!
56
57 @c FIXME::martin: Maybe add examples?
58
59 @cindex conditional evaluation
60 @cindex if
61 @cindex case
62 @cindex cond
63
64 Guile provides three syntactic constructs for conditional evaluation.
65 @code{if} is the normal if--then--else expression (with an optional else
66 branch), @code{cond} is a conditional expression with multiple branches
67 and @code{case} branches if an expression has one of a set of constant
68 values.
69
70 @deffn syntax if test consequent [alternate]
71 All arguments may be arbitrary expressions. First, @var{test} is
72 evaluated. If it returns a true value, the expression @var{consequent}
73 is evaluated and @var{alternate} is ignoret. If @var{test} evaluates to
74 @code{#f}, @var{alternate} is evaluated instead. The value of the
75 evaluated branch (@var{consequent} or @var{alternate}) is returned as
76 the value of the @code{if} expression.
77
78 When @var{alternate} is omitted and the @var{test} evaluates to
79 @code{#f}, the value of the expression is not specified.
80 @end deffn
81
82 @deffn syntax cond clause1 clause2 @dots{}
83 Each @code{cond}-clause must look like this:
84
85 @lisp
86 (@var{test} @var{expression} @dots{})
87 @end lisp
88
89 where @var{test} and @var{expression} are arbitrary expression, or like
90 this
91
92 @lisp
93 (@var{test} => @var{expression}
94 @end lisp
95
96 where @var{expression} must evaluate to a procedure.
97
98 The @var{test}s of the clauses are evaluated in order and as soon as one
99 of them evaluates to a true values, the corresponding @var{expression}s
100 are evaluated in order and the last value is returned as the value of
101 the @code{cond}--expression. For the @code{=>} clause type,
102 @var{expression} is evaluated and the resulting procedure is applied to
103 the value of @var{test}. The result of this procedure application is
104 then the result of the @code{cond}--expression.
105
106 The @var{test} of the last @var{clause} may be the keyword @code{else}.
107 Then, if none of the preceding @var{test}s is true, the @var{expression}s following the @code{else} are evaluated to produce the result of the @code{cond}--expression.
108 @end deffn
109
110 @deffn syntax case key clause1 clause2 @dots{}
111 @var{key} may be any expression, the @var{clause}s must have the form
112
113 @lisp
114 ((@var{datum1} @dots{}) @var{expr1} @var{expr2} @dots{})
115 @end lisp
116
117 and the last @var{clause} may have the form
118
119 @lisp
120 (else @var{expr1} @var{expr2} @dots{})
121 @end lisp
122
123 All @var{datum}s must be distinct. First, @var{key} is evaluated. The
124 the result of this evaluation is compared against all @var{datum}s using
125 @code{eqv?}. When this comparison succeeds, the epression(s) following
126 the @var{datum} are evaluated from left to right, returning the value of
127 the last expression as the result of the @code{case} expression.
128
129 If the @var{key} matches no @var{datum} and there is an
130 @code{else}--clause, the expressions following the @code{else} are
131 evaluated. If there is no such clause, the result of the expression is
132 unspecified.
133 @end deffn
134
135
136 @node and or
137 @section Conditional Evaluation of a Sequence of Expressions
138
139 @c FIXME::martin: Review me!
140
141 @c FIXME::martin: Maybe add examples?
142
143 @code{and} and @code{or} evaluate all their arguments, similar to
144 @code{begin}, but evaluation stops as soon as one of the expressions
145 evaluates to false or true, respectively.
146
147 @deffn syntax and expr @dots{}
148 Evaluate the @var{expr}s from left to right and stop evaluation as soon
149 as one expression evaluates to @code{#f}; the remaining expressions are
150 not evaluated. The value of the last evaluated expression is returned.
151 If no expression evaluates to @code{#f}, the value of the last
152 expression is returned.
153
154 If used without expressions, @code{#t} is returned.
155 @end deffn
156
157 @deffn syntax or expr @dots{}
158 Evaluate the @var{expr}s from left to right and stop evaluation as soon
159 as one expression evaluates to a true value (that is, a value different
160 from @code{#f}); the remaining expressions are not evaluated. The value
161 of the last evaluated expression is returned. If all expressions
162 evaluate to @code{#f}, @code{#f} is returned.
163
164 If used without expressions, @code{#f} is returned.
165 @end deffn
166
167
168 @node while do
169 @section Iteration mechanisms
170
171 @c FIXME::martin: Review me!
172
173 @c FIXME::martin: Maybe add examples?
174
175 @cindex iteration
176 @cindex looping
177
178 Scheme has only few iteration mechanisms, mainly because iteration in
179 Scheme programs is normally expressed using recursion. Nevertheless,
180 R5RS defines a construct for programming loops, calling @code{do}. In
181 addition, Guile has an explicit looping syntax called @code{while}.
182
183 @deffn syntax do ((variable1 init1 step1) @dots{}) (test expr @dots{}) command @dots{}
184 The @var{init} expressions are evaluated and the @var{variables} are
185 bound to their values. Then looping starts with testing the @var{test}
186 expression. If @var{test} evaluates to a true value, the @var{expr}
187 following the @var{test} are evaluated and the value of the last
188 @var{expr} is returned as the value of the @code{do} expression. If
189 @var{test} evaluates to false, the @var{command}s are evaluated in
190 order, the @var{step}s are evaluated and stored into the @var{variables}
191 and the next iteration starts.
192
193 Any of the @var{step} expressions may be omitted, so that the
194 corresponding variable is not changed during looping.
195 @end deffn
196
197 @deffn syntax while cond body @dots{}
198 Evaluate all expressions in @var{body} in order, as long as @var{cond}
199 evaluates to a true value. The @var{cond} expression is tested before
200 every iteration, so that the body is not evaluated at all if @var{cond}
201 is @code{#f} right from the start.
202 @end deffn
203
204
205 @node Continuations
206 @section Continuations
207
208 @rnindex call-with-current-continuation
209 @c FIXME::martin: Document me!
210 @deffn primitive call-with-current-continuation
211 @end deffn
212
213 @node Multiple Values
214 @section Returning and Accepting Multiple Values
215
216 @c FIXME::martin: Review me!
217 @cindex multiple values
218 @cindex receive
219
220 Scheme allows a procedure to return more than one value to its caller.
221 This is quite different to other languages which only allow
222 single--value returns. Returning multiple values is different from
223 returning a list (or pair or vector) of values to the caller, because
224 conceptionally not @emph{one} compound object is returned, but several
225 distinct values.
226
227 The primitive procedures for handling multiple values are @code{values}
228 and @code{call-with-values}. @code{values} is used for returning
229 multiple values from a procedure. This is done by placing a call to
230 @code{values} with zero or more arguments in tail position in a
231 procedure body. @code{call-with-values} combines a procedure returning
232 multiple values with a procedure which accepts these values as
233 parameters.
234
235 @rnindex values
236 @deffn primitive values expr @dots{}
237 Delivers all of its arguments to its continuation. Except for
238 continuations created by the @code{call-with-values} procedure,
239 all continuations take exactly one value. The effect of
240 passing no value or more than one value to continuations that
241 were not created by @code{call-with-values} is unspecified.
242 @end deffn
243
244 @rnindex call-with-values
245 @deffn primitive call-with-values producer consumer
246 Calls its @var{producer} argument with no values and a
247 continuation that, when passed some values, calls the
248 @var{consumer} procedure with those values as arguments. The
249 continuation for the call to @var{consumer} is the continuation
250 of the call to @code{call-with-values}.
251
252 @example
253 (call-with-values (lambda () (values 4 5))
254 (lambda (a b) b))
255 ==> 5
256
257 @end example
258 @example
259 (call-with-values * -) ==> -1
260 @end example
261 @end deffn
262
263 In addition to the fundamental procedures described above, Guile has a
264 module which exports a syntax called @code{receive}, which is much more
265 convenient. If you want to use it in your programs, you have to load
266 the module @code{(ice-9 receive)} with the statement
267
268 @lisp
269 (use-modules (ice-9 receive))
270 @end lisp
271
272 @deffn {library syntax} receive formals expr body @dots{}
273 Evaluate the expression @var{expr}, and bind the result values (zero or
274 more) to the formal arguments in the formal argument list @var{formals}.
275 @var{formals} must have the same syntax like the formal argument list
276 used in @code{lambda} (@pxref{Lambda}). After binding the variables,
277 the expressions in @var{body} @dots{} are evaluated in order.
278 @end deffn
279
280
281 @node Exceptions
282 @section Exceptions
283 @cindex error handling
284 @cindex exception handling
285
286 It is traditional in Scheme to implement exception systems using
287 @code{call-with-current-continuation}. Guile does not do this, for
288 performance reasons. The implementation of
289 @code{call-with-current-continuation} is a stack copying implementation.
290 This allows it to interact well with ordinary C code. Unfortunately, a
291 stack-copying implementation can be slow -- creating a new continuation
292 involves a block copy of the stack.
293
294 Instead of using @code{call-with-current-continuation}, the exception
295 primitives documented here are implemented as built-ins that take
296 advantage of the @emph{upward only} nature of exceptions.
297
298 @deffn primitive catch key thunk handler
299 Invoke @var{thunk} in the dynamic context of @var{handler} for
300 exceptions matching @var{key}. If thunk throws to the symbol
301 @var{key}, then @var{handler} is invoked this way:
302 @lisp
303 (handler key args ...)
304 @end lisp
305 @var{key} is a symbol or @code{#t}.
306 @var{thunk} takes no arguments. If @var{thunk} returns
307 normally, that is the return value of @code{catch}.
308 Handler is invoked outside the scope of its own @code{catch}.
309 If @var{handler} again throws to the same key, a new handler
310 from further up the call chain is invoked.
311 If the key is @code{#t}, then a throw to @emph{any} symbol will
312 match this call to @code{catch}.
313 @end deffn
314
315 @deffn primitive throw key . args
316 Invoke the catch form matching @var{key}, passing @var{args} to the
317 @var{handler}.
318
319 @var{key} is a symbol. It will match catches of the same symbol or of
320 #t.
321
322 If there is no handler at all, an error is signaled.
323 @end deffn
324
325 @deffn primitive lazy-catch key thunk handler
326 This behaves exactly like @code{catch}, except that it does
327 not unwind the stack (this is the major difference), and if
328 handler returns, its value is returned from the throw.
329 @end deffn
330
331
332 @node Error Reporting
333 @section Procedures for Signaling Errors
334
335 Guile provides a set of convenience procedures for signaling error
336 conditions that are implemented on top of the exception primitives just
337 described.
338
339 @c begin (scm-doc-string "boot-9.scm" "error")
340 @deffn procedure error msg args @dots{}
341 Raise an error with key @code{misc-error} and a message constructed by
342 displaying @var{msg} and writing @var{args}.
343 @end deffn
344 @c end
345
346 @deffn primitive scm-error key subr message args data
347 Raise an error with key @var{key}. @var{subr} can be a string
348 naming the procedure associated with the error, or @code{#f}.
349 @var{message} is the error message string, possibly containing
350 @code{~S} and @code{~A} escapes. When an error is reported,
351 these are replaced by formatting the corresponding members of
352 @var{args}: @code{~A} (was @code{%s} in older versions of
353 Guile) formats using @code{display} and @code{~S} (was
354 @code{%S}) formats using @code{write}. @var{data} is a list or
355 @code{#f} depending on @var{key}: if @var{key} is
356 @code{system-error} then it should be a list containing the
357 Unix @code{errno} value; If @var{key} is @code{signal} then it
358 should be a list containing the Unix signal number; otherwise
359 it will usually be @code{#f}.
360 @end deffn
361
362 @deffn primitive strerror err
363 Return the Unix error message corresponding to @var{err}, which
364 must be an integer value.
365 @end deffn
366
367 @c begin (scm-doc-string "boot-9.scm" "false-if-exception")
368 @deffn syntax false-if-exception expr
369 Returns the result of evaluating its argument; however
370 if an exception occurs then @code{#f} is returned instead.
371 @end deffn
372 @c end
373
374
375 @node Dynamic Wind
376 @section Dynamic Wind
377
378 [FIXME: this is pasted in from Tom Lord's original guile.texi and should
379 be reviewed]
380
381 @rnindex dynamic-wind
382 @deffn primitive dynamic-wind in_guard thunk out_guard
383 All three arguments must be 0-argument procedures.
384 @var{in_guard} is called, then @var{thunk}, then
385 @var{out_guard}.
386 If, any time during the execution of @var{thunk}, the
387 continuation of the @code{dynamic_wind} expression is escaped
388 non-locally, @var{out_guard} is called. If the continuation of
389 the dynamic-wind is re-entered, @var{in_guard} is called. Thus
390 @var{in_guard} and @var{out_guard} may be called any number of
391 times.
392 @lisp
393 (define x 'normal-binding)
394 @result{} x
395 (define a-cont (call-with-current-continuation
396 (lambda (escape)
397 (let ((old-x x))
398 (dynamic-wind
399 ;; in-guard:
400 ;;
401 (lambda () (set! x 'special-binding))
402 ;; thunk
403 ;;
404 (lambda () (display x) (newline)
405 (call-with-current-continuation escape)
406 (display x) (newline)
407 x)
408 ;; out-guard:
409 ;;
410 (lambda () (set! x old-x)))))))
411 ;; Prints:
412 special-binding
413 ;; Evaluates to:
414 @result{} a-cont
415 x
416 @result{} normal-binding
417 (a-cont #f)
418 ;; Prints:
419 special-binding
420 ;; Evaluates to:
421 @result{} a-cont ;; the value of the (define a-cont...)
422 x
423 @result{} normal-binding
424 a-cont
425 @result{} special-binding
426 @end lisp
427 @end deffn
428 @c Local Variables:
429 @c TeX-master: "guile.texi"
430 @c End: