common-lisp dotimes


Example

dotimes is a macro for integer iteration over a single variable from 0 below some parameter value. One of the simples examples would be:

CL-USER> (dotimes (i 5)
           (print i))

0 
1 
2 
3 
4 
NIL

Note that NIL is the returned value, since we did not provide one ourselves; the variable starts from 0 and throughout the loop becomes values from 0 to N-1. After the loop, the variable becomes the N:

CL-USER> (dotimes (i 5 i))
5

CL-USER> (defun 0-to-n (n)
           (let ((list ()))
             (dotimes (i n (nreverse list))
               (push i list))))
0-TO-N
CL-USER> (0-to-n 5)
(0 1 2 3 4)