common-lisp Basic loops dolist


Example

dolist is a looping macro created to easily loop through the lists. One of the simplest uses would be:

CL-USER> (dolist (item '(a b c d))
           (print item))

A 
B 
C 
D 
NIL ; returned value is NIL

Note that since we did not provide return value, NIL is returned (and A,B,C,D are printed to *standard-output*).

dolist can also return values:

;;This may not be the most readable summing function.
(defun sum-list (list)
  (let ((sum 0))
    (dolist (var list sum)
      (incf sum var))))

CL-USER> (sum-list (list 2 3 4))
9