rec defines a local tail-recursive function and immediately invokes it with initial arguments, similar to Scheme’s “named let”.
Syntax
(rec ⟨name⟩ (⟨binding-or-declare⟩*)
⟨body-element⟩*
⟨expr⟩)
;; ⟨binding-or-declare⟩ := (⟨var⟩ ⟨init-expr⟩)
;; | (declare ⟨var⟩ ⟨qualified-type⟩)
Semantics
recbinds⟨name⟩to a local recursive function whose positional parameters are the variables⟨var⟩from the binding list (in source order), and immediately calls⟨name⟩with the corresponding⟨init-expr⟩values.- Duplicate binding variable names in the
recbinding list are rejected. - Optional
(declare ⟨var⟩ ⟨qualified-type⟩)clauses in the binding list constrain the initial bindings and the corresponding parameters of⟨name⟩. To constrain the return type of therecexpression itself, wrap therecform inthe. - The body is an implicit
progn(supporting multiple expressions and shorthandletbindings), and may return a single value,Void, or multiple values. - Tail-call requirement: Every reference to
⟨name⟩inside the body must be a direct tail call(⟨name⟩ ⟨arg⟩*)in a tail position of therecbody (such as the final expression ofprogn,let,let*,cond,when,unless,and,or,match,catch,resumable, or branches ofif). Non-tail calls to⟨name⟩or passing⟨name⟩as a first-class value are rejected at compile time. - If you need non-tail recursion or need to pass the recursive function as a value, use
letwithfninstead.
Examples
;; Tail-recursive factorial accumulator
(coalton
(the Integer
(rec % ((declare i Integer)
(declare acc Integer)
(i 5)
(acc 1))
(if (<= i 1)
acc
(% (- i 1) (* acc i))))))
; => 120
;; Tail-recursive list search using `match`
(coalton-toplevel
(declare find-index (Eq :a => :a * (List :a) -> (Optional UFix)))
(define (find-index target xs)
(rec go ((rem xs)
(idx 0))
(match rem
((Nil) None)
((Cons x rest)
(if (== x target)
(Some idx)
(go rest (+ idx 1))))))))
Other notes
- By convention, short names such as
%,go, orloopare commonly used for⟨name⟩. - When the types of a
recloop are known, typeclass dictionaries supplied by context-free global instances do not become runtime parameters of its local recursive function (labels), so numeric loops inreccompile to tight loops comparable tofor. Polymorphic loops and contextual dictionary constructors retain their dictionary parameters as needed. See Performance and Optimization.