clausa
Translation status: 繁體中文 reader-locale proof. Code fences render through the zh-Hant pipeline; prose is canonical Latin.
Closure and lambda expressions with inferred or annotated types.
Aliases: lambda, closure
Syntax: <params> ∴ <expression> | (<params>) → <type> ∴ <expression>
Category#
function
Related#
Examples#
radix/corpus/clausa/clausa.fab (canonical · keyword)#
Closure and lambda expressions with inferred or annotated types.
# =============================================================================
# clausa — Closure and lambda expressions with inferred or annotated types.
# =============================================================================
#
# What this teaches:
# • Closure syntax — `<params> ∴ <expression>` defines an inline anonymous
# function with inferred return type
# • Typed closures — `(params) → <type> ∴ <expression>` adds explicit
# return type annotation
# • Higher-order usage — closures can be stored in variables, passed to
# iterators, and used as predicates
#
# Common mistakes:
# • confusing `clausa` (closure expression) with `functio` (named function declaration), or omitting parentheses on multi-parameter typed closures
#
# See also: ∴, functio, numerus
# =============================================================================
# clausa — closure / lambda expressions
#
# <params> ∴ <expr>
# (<params>) → <typus> ∴ <expr>
#
# GRAMMAR:
# closureExpr :← paramList '∴' expr | '(' paramList ')' '→' type '∴' expr
#
# EXPECTED OUTPUT:
# clausa.expected
main {
# Single-parameter closure: numerus x ∴ x * 2
const _ duplica ← int x ∴ x * 2
print duplica(5)
# Multi-parameter typed closure
const _ adde ← (int a, int b) → int ∴ a + b
print adde(3, 4)
const _ numeri ← [1, 2, 3]
var list<int> duplicati ← vacua
# predicate closure
const _ par ← int x → bool ∴ x % 2 ≡ 0
var list<int> pares ← vacua
for from numeri const n {
duplicati.appende(duplica(n))
if par(n) {
pares.appende(n)
}
}
print duplicati.longitudo()
print duplicati[0]
print duplicati[2]
print pares.longitudo()
print pares[0]
const _ positivus ← int n → bool ∴ n > 0
print positivus(10)
print positivus(-5)
}Expected output:
10
7
3
2
6
1
2
verum
falsum