Files
AILang/examples/bench_list_sum.prose.txt
T
Brummel 9cf0e3e81c prose: infix + paren elision + doc-wrap + nested-match (iter 20b)
Eleven binary builtins (+ - * / % == != < <= > >=) render as
lhs op rhs when called with two args (tail-flagged keeps prefix).
Standard Rust-aligned 4-level precedence table elides parens.
not(x) renders as !x. Long /// doc strings wrap at 80 cols on
word boundaries.

bench_list_sum: 'if ==(n, 0)' becomes 'if n == 0', '+(acc, h)'
becomes 'acc + h', tail keyword still visible on tail calls.

19 new unit tests (47 total). 4 snapshots (3 re-rendered + 1 new
bench fixture). Public API unchanged.
2026-05-08 18:13:43 +02:00

42 lines
871 B
Plaintext

// module bench_list_sum
data IntList = INil | ICons(Int, IntList)
/// Tail-recursive list builder. Result = accumulator-prepended list.
fn cons_n_acc(n: Int, acc: IntList) -> IntList {
if n == 0 {
acc
} else {
tail cons_n_acc(n - 1, ICons(n - 1, acc))
}
}
/// Build [0, 1, ..., n-1] :: IntList. Order doesn't matter for sum.
fn cons_n(n: Int) -> IntList {
cons_n_acc(n, INil)
}
/// Tail-recursive sum.
fn sum_acc(xs: IntList, acc: Int) -> Int {
match xs {
INil => acc,
ICons(h, t) => tail sum_acc(t, acc + h)
}
}
/// Sum every element. Calls sum_acc with seed 0.
fn sum_list(xs: IntList) -> Int {
sum_acc(xs, 0)
}
/// Build a list of length n, sum it, print the sum.
fn run_one(n: Int) -> Unit with IO {
do io/print_int(sum_list(cons_n(n)))
}
fn main() -> Unit with IO {
run_one(100000);
run_one(1000000);
run_one(3000000)
}