// Allocation: build and walk many binary trees (after the Computer // Language Benchmarks Game's binary-trees). Each tree holds its round's // number, so that no compiler builds it only once. import std/io type Tree { Leaf(Int), Node(Tree, Tree) } fn make :: d: Int v: Int -> Tree { if d == 0 { Leaf(v) } else { Node(make (d - 1) v, make (d - 1) v) } } fn check :: t: Tree -> Int { match t { Leaf(v) -> v % 2, Node(l, r) -> 1 + check l + check r } } fn pow2 :: k: Int -> Int { if k == 0 { 1 } else { 2 * pow2 (k - 1) } } fn repeat :: i: Int d: Int acc: Int -> Int { if i == 0 { acc } else { repeat (i - 1) d (acc + check (make d i)) } } fn depths :: d: Int max: Int acc: Int -> Int { if d > max { acc } else { depths (d + 2) max (acc + repeat (pow2 (max - d + 4)) d 0) } } pub fn main :: -> () / { io.println (show (depths 4 16 0)) }