
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | // Allocation: build and walk many binary trees (after the Computer |
| 2 | // Language Benchmarks Game's binary-trees). Each tree holds its round's |
| 3 | // number, so that no compiler builds it only once. |
| 4 | |
| 5 | import std/io |
| 6 | |
| 7 | type Tree { Leaf(Int), Node(Tree, Tree) } |
| 8 | |
| 9 | fn make :: d: Int v: Int -> Tree |
| 10 | { |
| 11 | if d == 0 |
| 12 | { |
| 13 | Leaf(v) |
| 14 | } |
| 15 | else |
| 16 | { |
| 17 | Node(make (d - 1) v, make (d - 1) v) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn check :: t: Tree -> Int |
| 22 | { |
| 23 | match t { Leaf(v) -> v % 2, Node(l, r) -> 1 + check l + check r } |
| 24 | } |
| 25 | |
| 26 | fn pow2 :: k: Int -> Int |
| 27 | { |
| 28 | if k == 0 |
| 29 | { |
| 30 | 1 |
| 31 | } |
| 32 | else |
| 33 | { |
| 34 | 2 * pow2 (k - 1) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | fn repeat :: i: Int d: Int acc: Int -> Int |
| 39 | { |
| 40 | if i == 0 |
| 41 | { |
| 42 | acc |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | repeat (i - 1) d (acc + check (make d i)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn depths :: d: Int max: Int acc: Int -> Int |
| 51 | { |
| 52 | if d > max |
| 53 | { |
| 54 | acc |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | depths (d + 2) max (acc + repeat (pow2 (max - d + 4)) d 0) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn main :: -> () / <io> |
| 63 | { |
| 64 | io.println (show (depths 4 16 0)) |
| 65 | } |