Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

Using generators in Icon

Tech Computer Center logo

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
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: A tutorial for the Icon programming language
Previous: The joys of failure in Icon
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman,
Last updated: 1996/01/06 21:12:57 UT
QR two-dimensional bar code