Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Created December 14, 2024 00:20
Show Gist options
  • Save pchiusano/328830477d1d23f8167a30c678dea45d to your computer and use it in GitHub Desktop.
Save pchiusano/328830477d1d23f8167a30c678dea45d to your computer and use it in GitHub Desktop.
Skew K-ary tree
-- Structure: a buffer of Arity, then a list of complete trees of exponentially increasing size
type Skew a = Skew [a] [(Nat, [Skew.Tree a])]
type Skew.Tree a = Tree [a] [Skew.Tree a]
Skew.Arity = 4
Skew.cons : a -> Skew a -> Skew a
Skew.cons a = cases Skew hd spine ->
hd' = a +: hd
if List.size hd' < Arity then Skew hd' spine
else match spine with
[] -> Skew [] [(Arity, [Tree hd' []])]
(sz, forest) +: tl
| List.size forest == Arity ->
sz' = sz * Arity + Arity
t = Tree hd' forest
match tl with
(sz2, forest2) +: tl | sz2 == sz' ->
forest2' = t +: forest2
Skew [] ((sz', forest2') List.+: tl)
_ -> Skew [] ((sz', [t]) +: tl)
| sz == Arity -> Skew [] ((sz, Tree hd' [] +: forest) +: tl)
| otherwise -> Skew [] ((Arity, [Tree hd' []]) +: spine)
> List.foldRight Skew.cons (Skew [] []) (range 0 64)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment