summaryrefslogtreecommitdiff
path: root/examples/recursion.ts
blob: 8ee899f0fb6845587d0bbb0052c0310cb320d0d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.