Instead of using FOR, it is always possible to recurse on the TL of the list. The recursive technique has the great advantage that successive calls of the procedure can be TRACEd. Constructions utilising loops (REPEAT, WHILE, UNTIL, FOR) do not admit of easy monitoring through TRACE.
Often, however, it is very quick and easy, using VED, to temporarily alter the definition so that each time round the loop the procedure prints something (e.g. the value of `x' in the last example). In a system such as Poplog altering and recompiling a procedure is so fast that temporary changes for debugging purposes can be very much more useful than in a conventional language.
Moreover, if a looping construct is used, then the list brackets [ ... ] ( or the vector brackets { ..... } ) can be used to collect together all the items left on the stack into a list, if that is desired. (This is one of the features of Pop-11 not available in LISP, which uses alternative mechanisms.) Compare the following two different definitions of procedures equivalent to maplist.
define maplist1(list, proc); if null(list) then [] else proc(hd(list)) :: maplist1(tl(list), proc) endif enddefine; define maplist2(list, proc); lvars item; [%for item in list do proc(item) endfor%] enddefine;The former uses the so-called "functional" style of programming. However, I have no doubt that the second is easier to understand and that programmers using the second style will make fewer mistakes.
Another example: here are two ways make a list of all the non-empty tails of a list:
define all_tails1(list); if null(list) then [] else list :: all_tails1(tl(list)) endif enddefine; define all_tails2(list); lvars tail; [%for tail on list do tail endfor%] enddefine; all_tails1([ a b c d ]) => ** [[a b c d] [b c d] [c d] [d]]An advantage of the recursive version is that when it is traced it shows clearly what is going on, whereas extra printing instructions have to be put into programs using for ... endfor to show what is happening on each iteration.
Try
trace all_tails1;then redo the above command. (See TEACH TRACE)
Using the integrated editor VED makes it very quick and easy to add or remove printing instructions and recompile, so the difference in traceability is not very great.