News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


Using generators in Icon

The Icon expression ``1 to 5'' is a simple example of a generator expression, that is, one that produces more than one result.

You can write procedures that act as generators. In this example, Two_Powers is a procedure that takes two arguments a and b and generates the sequence of powers of two, from 2 to the a power to 2 to the b power:

    procedure Two_Powers ( a, b )
    #--Generates 2^a, 2^(a+1), ..., 2^b
      local i

      every  i := a to b  do     # Let i = a, a+1, a+2, ..., b
        suspend  2^i;            # Each time, return 2^i

      fail;                      # No more results; fail
    end
The statement ``suspend e1'' means ``return e1 as the next value of this procedure.'' The fail statement means ``there are no more values to return.''
Next: A quadratic root solver in Icon
See also: Using the Icon programming language
Previous: The joys of failure in Icon

John W. Shipman, john@nmt.edu