Generating the elements of an Icon list |
|
Icon's ``!'' operator, applied to a list, generates
the elements of that list. For example, this expression would write
each element of list ``L'' on a separate line:
every write ( ! L );
Here's another example. This program reverses the lines from its input
by pushing them onto a stack, and then generating the stack's elements:
procedure main()
L := []; # Create an empty list
while push ( L, read ( ) ); # Push each line
every write ( ! L ); # Generate & write lines
end
The ``while'' loop reads each lines and adds it to
the front of the list ``L''. The ``!''
operator generates the elements of the list one at a time, and
the ``every'' iterates over the generated values as
they are written.