examples: lisp — proper tail calls in eval
This commit is contained in:
@@ -263,6 +263,18 @@ export fn main() i32 = {
|
||||
check_int ("let-product", "(let ((a 3) (b 4)) (* a b))", 12i64, ep);
|
||||
check_int ("begin-last", "(begin 1 2 (+ 10 20))", 30i64, ep);
|
||||
|
||||
// ---- tail-call optimization ----
|
||||
// 30k iterations is well past the pre-TCO segfault threshold
|
||||
// (~25k) but still completes inside the test budget. Each probe
|
||||
// exercises a different tail position: lambda body via if,
|
||||
// lambda body via begin, and lambda body via let.
|
||||
run ("def-spin", "(define spin (lambda (n a) (if (= n 0) a (spin (- n 1) (+ a 1)))))", ep);
|
||||
check_int ("tco-if", "(spin 30000 0)", 30000i64, ep);
|
||||
run ("def-bspin", "(define bspin (lambda (n a) (if (= n 0) a (begin a (bspin (- n 1) (+ a 1))))))", ep);
|
||||
check_int ("tco-begin", "(bspin 30000 0)", 30000i64, ep);
|
||||
run ("def-lspin", "(define lspin (lambda (n a) (if (= n 0) a (let ((m (- n 1))) (lspin m (+ a 1))))))", ep);
|
||||
check_int ("tco-let", "(lspin 30000 0)", 30000i64, ep);
|
||||
|
||||
// ---- floats ----
|
||||
check_float("float-add", "(+ 1.5 2.5)", 4.0, ep);
|
||||
check_float("float-mul", "(* 0.5 0.5)", 0.25, ep);
|
||||
|
||||
Reference in New Issue
Block a user