6.1.5 Loops and State

Loops are not only inspired by stateful programming; they are also particularly useful in the context of stateful data structures. For instance, we can use stacks to accumulate the results of a loop:

<reverse>=
<declare NewStack> 
declare fun{Reverse List}
           Acc = {NewStack}
        in 
           for X in List do {Acc.push X} end 
           {Acc.toList}
        end 
{Inspect {Reverse [1 2 3 4]}} 


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)