The 'dolist' special form is basically a list-oriented 'for' looping construct that contains a loop 'symbol', a 'list-expr' to draw values from, an optional 'return' value and a block of code [expressions] to evaluate. The sequence of execution is:
symbol := CAR of list-expr temp-list := CDR of list-expr while temp-list is not empty loop code execution symbol := CAR of temp-list temp-list := CDR of temp-list end-while return result
The main loop 'symbol' will take on successive values from 'list-expr'. The 'dolist' form will go through and create and initialize the 'symbol'. After execution of the loop 'exprs', the 'symbol' is set to the next value in the 'list-expr'. This continues until the 'list-expr' has been exhausted. The value of the 'result' expression is evaluated and returned. If no 'result' is specified, NIL is returned. When the 'dolist' is finished execution, the 'symbol' that was defined will no longer exist or retain its value. If the 'list-expr' is an empty list, then no loop execution takes place and the 'result' is returned.
(dolist (i () "done") ; DOLIST with I loop variable (print "here")) ; an empty list ; and a return value ; returns "done" (dolist (x '(a b c) "fini") ; DOLIST with X loop variable (princ x)) ; a list with (A B C) ; and a return value ; prints ABC returns "fini" (dolist (y '(1 2 3)) ; DOLIST with Y loop variable (princ (* y y))) ; a list with (1 2 3) ; and no return value ; prints 149 returns NIL ; returns "met in the middle"
See the
dolist
special form in the