6.1.2 Loops by Recursion

Conceptually, we can think of the variable of a loop to be mutable. But of course, variables are not mutable in Oz. So how are these two statement compatable?

Another way of looking at loops is as recursive procedures. These procedures apply the body of the loop to all elements of a list the loop iterates over. For instance, the loop above becomes the following procedure:

<loop by recursion>=
local 
   proc{AboveLoop Xs}
      case Xs  
      of nil the skip 
      [] X|Xss then {Inspect X} {AboveLoop Xss}
      end 
   end    
in 
   {AboveLoop [a b c]}
end

Indeed, this is that way that loops are implemented in Oz. The expansion of loops into the core syntax turns them into recursive procedures.


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