Special Form Entry: FOREACH
← Prev | ↑ Chapter | Next → | Index | Symbols
Special Form Entry: FOREACH
Name
foreach
Class
Special Form
Syntax
(foreach name list [expr ...])
Description
Iterates over each element of list, binding it to name.
Evaluation Rules
- evaluate
list - for each element of the resulting list, bind it to
name - evaluate the body forms in order
Return Values
Returns the final body value or nil if no body exists.
Vendor Divergence
A foreach with no body at all is the one measured disagreement
between the two hosts:
| form | AutoCAD 2026 | BricsCAD V25/V26 |
|---|---|---|
(foreach e '(1 2 3)) |
nil |
(1 2 3) |
AutoCAD follows the rule stated above: with no body there is no final body value, so the result is nil (AutoCAD 2026 by probe; AutoCAD 2022 confirmed nil at the console). BricsCAD returns the list, on macOS and on Windows alike, which is the shape an implementation produces when the loop's accumulator is seeded with the sequence and the body never overwrites it. No vendor page documents either answer.
Every other behaviour in this entry — the binding rule, the value of a loop with a body, the value over an empty list — was measured identical on both hosts.
clautolisp
Returns nil for a body-less foreach by default — under --strict,
--autocad and --clautolisp — following the rule stated under Return
Values and AutoCAD (2022 and 2026 both measured nil). The return is
now DISCRIMINATED BY DIALECT, like every other recognised vendor
divergence: under --dialect bricscad (and --lax) it returns the
list, matching BricsCAD. --dialect bricscad and --strict also emit
a [foreach-empty-body] portability warning, since the two answers are
not portable. An empty list returns nil under every dialect — the
hosts agree there, so nothing diverges and nothing warns. Implemented in
clautolisp/autolisp-runtime/source/api.lisp (FOREACH-EMPTY-BODY-RESULT),
called by both the interpreter and the compiler so the two cannot drift;
see issues/closed/foreach-empty-body-return-diverges.issue.
Side Effects
Side effects are those of:
- evaluation of
list, - rebinding
namefor each iteration, - evaluation of the body forms for each iteration.
Exceptional Situations
Erroneous if the list argument is not acceptable to the active host semantics.
Availability
- AutoCAD 2026: documented (per Autodesk reference page), and verified by probe 2026-08-31 (Windows).
- BricsCAD V25/V26: verified by probe 2026-08-31 (macOS and Windows), no longer presumed. Compatible except for the no-body return value recorded under Vendor Divergence.
- Status: both hosts measured. Evidence:
probe-results/{autocad/ms-windows,bricscad/ms-windows,bricscad/macos}/2026-08-31*, produced byprobes/sources/probe-foreach-scope.lsp.
Source Notes
- [foreach (AutoLISP)](https://help.autodesk.com/cloudhelp/2022/ENU/AutoCAD-AutoLISP-Reference/files/GUID-BE6A23C4-4E18-45A6-854E-2DE9574A6925.htm)
- Status: documented in core behavior; concise here.