REDUCE

21.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 \(\langle \)id:identifier\(\rangle \) (in\(\ \mid \ \)on) \(\langle \)lst:list\(\rangle \) (do\(\ \mid \ \)collect\(\ \mid \ \)join\(\ \mid \ \)product\(\ \mid \ \)sum)\(\langle \)exprn:S-expr\(\rangle \)

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




in mapc mapcar mapcan
on map maplist mapcon




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

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


Hosted by Download REDUCE Powered by MathJax