From cc8eeeb482f279c80ebef17fc5968e73a51b48b8 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Thu, 6 Nov 2025 21:12:44 -0800 Subject: add examples --- examples/recursion.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 examples/recursion.ts (limited to 'examples/recursion.ts') diff --git a/examples/recursion.ts b/examples/recursion.ts new file mode 100644 index 0000000..8ee899f --- /dev/null +++ b/examples/recursion.ts @@ -0,0 +1,18 @@ +/* +Any practical programming language can of course handle recursion, and KaiScript is no exception. +Factorial is a function that is particularly easy to define recursively. +*/ + +import { createFn } from "../src"; + +// bind(name, fn) returns fn inside a closure containing itself bound to name +const factorial = createFn<[number]>()( + `bind(fac,fn(n,?(eq(n, 1),n,mul(n,call(fac,sub(n,1))))))` +); + +// const factRes: 720 +const factRes = factorial(6); +console.log(factRes); + +// You might be disappointed to learn that computing factorials larger than 6 +// fails catastrophically. Blame TypeScript. -- cgit v1.2.3-70-g09d2