Skip to content

Instantly share code, notes, and snippets.

@KevOrr
Last active January 29, 2018 11:15
Show Gist options
  • Save KevOrr/db0029ed14f3c00fc6b8cd972a8fc937 to your computer and use it in GitHub Desktop.
Save KevOrr/db0029ed14f3c00fc6b8cd972a8fc937 to your computer and use it in GitHub Desktop.
64-deep binary tree
data Tree a = Leaf a | Node (Tree a) (Tree a)
data Direction = L | R
make :: Integer -> Tree Int
make 1 = Leaf 0
make n = Node (make (n - 1)) (make (n - 1))
get :: Tree a -> [Direction] -> a
get (Node x _) (L:idxs) = get x idxs
get (Node _ y) (R:idxs) = get y idxs
get (Leaf x) nil = x
set :: Tree a -> [Direction] -> a -> Tree a
set (Node x y) (L:idxs) val = Node (set x idxs val) y
set (Node x y) (R:idxs) val = Node x (set y idxs val)
set (Leaf _) nil val = Leaf val
main = do
putStrLn (let tree = make 64
newtree = set tree [L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L,
L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L] 1
in show $ get newtree [L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L,
L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L, L, L, R, L])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment