man_or_boy_14.fz
// using man_or_boy as a simple performance benchmark
//
// This calculates Knuth's man-or-boy function for the values 0..14
// repeatedly 200 times.
//
// Note that this is heavily recursive, it requires a large stack.
man_or_boy_14 =>
a(k i32, x1, x2, x3, x4, x5 () -> i32) i32 is
b => set k := k - 1; a k (() -> b) x1 x2 x3 x4
if k <= 0 x4() + x5() else b
K(n i32) => fun n
test(num, max, expected i32) i32
post
test.this.result = expected // NYI: misusing postcondition, check does not work yet
is
res := 0
for i in 1..num do
(0..max) | (n) ->
set res := res + a(n, K( 1), K(-1), K(-1), K( 1), K( 0))
say "$num * sum (n in 0..$max: man_or_boy(n)) = $res"
res
test(200, 14, -524800)
last changed: 2023-04-11