type: special form (fsubr) location: built-in source file: xlcont.c Common LISP compatible: yes supported on: all machines
(dolist ( <symbol> <list-expr> [ <result> ] ) [ <expr> ... ] ) <symbol> - a symbol <list-expr> - a list expression <result> - an optional expression for the returned result <expr> - expressions comprising the body of the loop which may contain RETURNs, GOs or tags for GO
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 <expr>'s, 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"