1<?xml version='1.0'?>
2<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3  "/dtd/4.1.2/docbookx.dtd">
4<section id="dsssl.expr.loop"><title>Loops</title>
5<para>
6<indexterm><primary>for loop, DSSSL and</primary></indexterm>
7<indexterm><primary>loops, implementing (DSSSL)</primary></indexterm>
8<indexterm><primary>tail recursion (DSSSL)</primary></indexterm>
9
10<acronym>DSSSL</acronym> doesn't have any construct that resembles the
11for loop that occurs in most imperative languages like C
12and Java. Instead, <acronym>DSSSL</acronym> employs a common trick in
13functional languages for implementing a loop: tail recursion.
14</para>
15<para>
16Loops in <acronym>DSSSL</acronym> use a special form of
17<literal>let</literal>. This loop counts from 1 to 10:
18<screen>
19(let <co id="dl1"/>loopvar <co id="dl2"/>((count 1))
20  <co id="dl3"/>(if (> count 10)
21    <co id="dl4"/>#t
22    (<co id="dl5"/>loopvar <co id="dl6"/>(+ count 1))))</screen></para>
23<calloutlist>
24<callout arearefs="dl1">
25<para>This variable controls the loop. It is declared without an
26initial value, immediately after the <literal>let</literal>
27operand.</para>
28</callout>
29<callout arearefs="dl2">
30<para>
31<indexterm><primary>variables (DSSSL)</primary>
32  <secondary>local, defining after loop variable</secondary></indexterm>
33
34Any number of additional local variables can be defined after
35the loop variable, just as they can in any other
36<literal>let</literal> expression.</para>
37</callout>
38<callout arearefs="dl3">
39<para>If you ever want the loop to end, you have to put some sort of a
40test in it.</para>
41</callout>
42<callout arearefs="dl4">
43<para>This is the value that will be returned.</para>
44</callout>
45<callout arearefs="dl5">
46<para>Note that you iterate the loop by using the loop variable as if
47it was a function name.</para>
48</callout>
49<callout arearefs="dl6">
50<para>The arguments to this function are the values that
51you want the local variables declared in <xref linkend="dl2"/> to have
52in the next iteration.</para>
53</callout>
54</calloutlist>
55</section>
56