for is Coalton’s imperative loop form with parallel step updates.
Syntax
(for ⟨label⟩? (⟨binding-or-declare⟩*)
[:returns ⟨return-expr⟩]
[{:while | :until | :repeat} ⟨termination-expr⟩]
⟨body-element⟩*)
;; ⟨label⟩ := :⟨keyword-name⟩
;; ⟨binding-or-declare⟩ := (⟨var⟩ ⟨init-expr⟩ ⟨step-expr⟩)
;; | (declare ⟨var⟩ ⟨qualified-type⟩)
Semantics
- Label: An optional keyword
⟨label⟩(such as:outer) names the loop so nestedbreakandcontinueforms can target it. - Bindings:
- The binding list
(⟨binding-or-declare⟩*)is required, though it may be empty(). - Each binding
(⟨var⟩ ⟨init-expr⟩ ⟨step-expr⟩)requires both an initializer and a step expression. Write⟨var⟩itself as⟨step-expr⟩when a binding should remain constant across iterations. ⟨init-expr⟩and⟨step-expr⟩must unify to the same single-value type, and optional(declare ⟨var⟩ ⟨qualified-type⟩)clauses constrain the named loop variables.- Initializers are evaluated once before the first iteration using
let-style dependency ordering. - At the end of each iteration, all
⟨step-expr⟩forms are evaluated in parallel (viacl:psetq) from the current iteration’s bindings. Usefor*when later steps should observe earlier updated bindings in the same step phase.
- The binding list
- Return value (
:returns):- If present,
:returns ⟨return-expr⟩must appear immediately after the binding list. ⟨return-expr⟩is evaluated in the scope of the loop bindings when the loop exits (either when the termination clause stops the loop or whenbreakis executed), and its value(s) (a single value,Void, or multiple values) are returned from theforform.- When
:returnsis omitted, theforexpression has typeVoid(zero values).
- If present,
- Termination clause:
- At most one of
:while,:until, or:repeatmay follow:returns(or the binding list)::while ⟨boolean-expr⟩: tested before each iteration; the loop runs while the condition isTrue.:until ⟨boolean-expr⟩: tested before each iteration; the loop stops as soon as the condition isTrue.:repeat ⟨ufix-expr⟩:⟨ufix-expr⟩(of typeUFix) is evaluated once before the first iteration (in the loop binding scope), and the loop runs that many iterations.
- With no termination clause, the loop runs indefinitely until exited via
break,return,throw, orresume-to.
- At most one of
- Body:
⟨body-element⟩*forms are evaluated sequentially on each iteration (and may include shorthandletbindings); their values are discarded.
Examples
;; Parallel step update computing Fibonacci numbers
(coalton
(for ((declare i UFix)
(i 0 (+ i 1))
(a 0 b)
(b 1 (+ a b)))
:returns a
:while (< i 10)))
; => 55
;; Fixed-count `:repeat` loop with an early `:outer` break
(coalton
(progn
(let total = (coalton/cell:new (the UFix 0)))
(for :outer ()
:repeat 5
(for ((j 0 (+ j 1)))
:repeat 5
(when (> (coalton/cell:read total) 12)
(break :outer))
(coalton/cell:update! ƒs.(+ s j) total)))
(coalton/cell:read total)))
; => 13
Other notes
- In release mode, a local
coalton/cell:Cellaccumulator created withcell:new(either outside the loop or as an invariantforbinding(acc (cell:new init) acc)) and mutated or read inside aforloop (viacell:read,cell:write!,cell:swap!,cell:update!,cell:increment!,cell:decrement!,cell:push!, orcell:pop!) is unboxed into a typed mutable lexical variable when theCelldoes not escape its defining function. See Performance and Optimization. - See
for*,break,continue, andrec.