Kết xuấtvi

clausa

Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.

Closure and lambda expressions with inferred or annotated types.

Aliases: lambda, closure

Syntax: <params> ∴ <expression> | (<params>) → <type> ∴ <expression>

Category#

function

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 `hàm` (named function declaration), or omitting parentheses on multi-parameter typed closures
#
# See also: ∴, hàm, số
# =============================================================================

# clausa — closure / lambda expressions
#
# <params> ∴ <expr>
# (<params>) → <kiểu_tên> ∴ <expr>
#
# GRAMMAR:
#   closureExpr :← paramList '∴' expr | '(' paramList ')' '→' type '∴' expr
#
# EXPECTED OUTPUT:
#   clausa.expected

main {
    # Single-parameter closure: số x ∴ x * 2
    const (int) → int duplica ← int x ∴ x * 2
    print duplica(5)

    # Multi-parameter typed closure
    const (int, int) → int adde ← (int a, int b) → int ∴ a + b
    print adde(3, 4)
    const list<int, 3> numeri ← [1, 2, 3]
    var list<int> duplicati ← empty
    # predicate closure
    const (int) → bool paritas ← int x → bool ∴ x % 2 ≡ 0
    var list<int> pares ← empty
    for from numeri const n {
        duplicati.append(duplica(n))
        if paritas(n) {
            pares.append(n)
        }
    }
    print duplicati.length()
    print duplicati[0]
    print duplicati[2]
    print pares.length()
    print pares[0]
    const (int) → bool plus ← int n → bool ∴ n ≻ 0
    print plus(10)
    print plus(-5)
}

Expected output:

10
7
3
2
6
1
2
verum
falsum