REDUCE

17.6 FOR EACH Statement

The FOR EACH form of the FOR statement, designed for iteration down a list, is more general in symbolic mode. Its syntax is:

        FOR EACH ID:identifier {IN|ON} LST:list  
            {DO|COLLECT|JOIN|PRODUCT|SUM} EXPRN:S-expr

As in algebraic mode, if the keyword IN is used, iteration is on each element of the list. With ON, iteration is on the whole list remaining at each point in the iteration. As a result, we have the following equivalence between each form of FOR EACH and the various mapping functions in Lisp:





DO COLLECT JOIN




INMAPC MAPCARMAPCAN
ONMAP MAPLISTMAPCON




Example: To list each element of the list (a b c):

        for each x in ’(a b c) collect list x;