Next: , Previous: , Up: iklib syntaxes loops   [Index]


6.8.7.6 Iterating with the dolist syntax

Syntax: dolist (?var ?list-expr) ?body0 ?body
Syntax: dolist (?var ?list-expr ?result-expr) ?body0 ?body

Iterate of the list returned by ?list-expr binding, at each iteration, the next item from the list to ?var; evaluate the ?body forms in the region of the binding.

If ?result-expr is present: it is evaluated, in the region of ?var, to produce the return values. If ?result-expr is not present: the syntax use returns no values.

If ?list-expr returns null and ?result-expr is present: when ?result-expr is evaluated, ?var is bound to null.

This syntax meant to be similar to the Common Lisp syntax of the same name.

(import (vicare) (vicare checks))

(with-result
  (dolist (A '(1 2 3))
    (add-result A)))
⇒ (#!void (1 2 3))

(with-result
  (dolist (A '(1 2 3) A)
    (add-result A)))
⇒ (() (1 2 3))

(with-result
  (let ((rv #f))
    (dolist (A '(1 2 3) rv)
      (add-result A)
      (when (even? A)
        (set! rv A)))))
⇒ (2 (1 2 3))

(with-result
  (dolist (A '() A)
    (add-result A)))
⇒ (() ())