Direct Functions in Depth¶
The beginner tutorial introduced dfns. This tutorial covers the details: scoping rules, guard patterns, recursion techniques, and common idioms.
Scoping rules¶
Dfns use lexical scope. A variable assigned inside a dfn is local. An unassigned name looks outward through enclosing dfns to the workspace:
y is local to f. x is found in the workspace.
Nested dfns see their enclosing scope:
The inner dfn captures scale from outer. This is a closure.
Guard patterns¶
Multiple conditions¶
Guards are checked top to bottom. The first true guard returns:
The last expression (no guard) is the default — reached only if all guards are false.
Guards with scalar arguments only¶
Guards expect a scalar boolean (0 or 1). If the condition produces an array, this is an error.
Recursion patterns¶
Simple recursion¶
Accumulator pattern¶
The default ⍺←1 lets it be called monadically. The left argument accumulates the result.
Recursion on arrays¶
Common idioms¶
Identity / default¶
Pipeline style¶
Each step assigns to data, threading the computation through.
Anonymous dfns¶
Dfns don't have to be named:
Key points¶
- Dfns use lexical scope — inner dfns can capture variables from outer dfns (closures)
- Guards are checked top to bottom; the last unguarded expression is the default
∇enables recursion; use an accumulator pattern for tail-style recursion- Dfns are values: assign them, pass them to operators, use them inline
Next: Direct Operators