SML/NJ Library Manual


The Iterate structure


Synopsis

signature ITERATE
structure Iterate : ITERATE

The Iterate structure provides wrappers for simple forms of iteration.


Interface

val iterate : ('a -> 'a) -> int -> 'a -> 'a
val repeat : ((int * 'a) -> 'a) -> int -> 'a -> 'a
val for : ((int * 'a) -> 'a) -> (int * int * int) -> 'a -> 'a

Description

iterate f cnt a
invokes the function f cnt times, starting with the value a, and using the result of one iteration as the argument in the next. Recursively, we have
          iterate f cnt v = iterate f (cnt-1) (f v)
          iterate f 0 v = v
          
Raises the Fail exception if cnt is negative.

repeat f cnt a
is similar to iterate except that the function f is also passed the iteration index. In particular, it is equivalent to:
          #2(iterate (fn (i,v) => (i+1,f(i,v))) cnt (0,init))
          


for f (start, stop, inc) a
provides more control over the range and step-size involved in the interation. Recursively, we have
          for f (start,stop,inc) a = for f (start+inc,stop,inc) (f(start,a))
          
halting when start > stop for negative inc and halting when stop > start for positive inc. If inc is zero, start and ~TOP must be equal, and we have
          for f (start,stop,0) a = f(start,a)
          
Raises Fail if either inc is non-positive and stop > start, or if inc is non-negative and start > stop.



[ Top | Parent | Contents | Index | Root ]

Last Modified June 10, 1998
Comments to John Reppy
Copyright © 1998 Bell Labs, Lucent Technologies