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. 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, following AutoCAD and the rule stated under Return Values. Adopting one vendor's undocumented answer as the default of a dialect-neutral implementation would be backwards; whether --dialect bricscad should return the list instead is an open question, recorded in issues/open/foreach-empty-body-return-diverges.issue.

Side Effects

Side effects are those of:

  • evaluation of list,
  • rebinding name for 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 by probes/sources/probe-foreach-scope.lsp.

Source Notes