| www.delorie.com/gnu/docs/guile/guile_294.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Scheme has only few iteration mechanisms, mainly because iteration in
Scheme programs is normally expressed using recursion. Nevertheless,
R5RS defines a construct for programming loops, calling do. In
addition, Guile has an explicit looping syntax called while.
do expression. If
test evaluates to false, the commands are evaluated in
order, the steps are evaluated and stored into the variables
and the next iteration starts.
Any of the step expressions may be omitted, so that the corresponding variable is not changed during looping.
#f right from the start.
Another very common way of expressing iteration in Scheme programs is the use of the so-called named let.
Named let is a variant of let which creates a procedure and calls
it in one step. Because of the newly created procedure, named let is
more powerful than do--it can be used for iteration, but also
for arbitrary recursion.
let (see section 25.2 Local Variable Bindings).
Named let works as follows:
let) to variable. The
new procedure's formal argument names are the name of the
variables.
The next example implements a loop which iterates (by recursion) 1000 times.
(let lp ((x 1000))
(if (positive? x)
(lp (- x 1))
x))
=>
0
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |