Skip to content

Instantly share code, notes, and snippets.

@TomasDrozdik
Created September 16, 2018 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomasDrozdik/5af0562e252b7adf50f1cc8bbfca95ea to your computer and use it in GitHub Desktop.
Save TomasDrozdik/5af0562e252b7adf50f1cc8bbfca95ea to your computer and use it in GitHub Desktop.
data Tree a = Tree a [Tree a] deriving Show
label :: Tree a -> Tree (a, Int)
label t = label' 1 t
label' :: Int -> Tree a -> Tree (a, Int)
label' i (Tree x []) = Tree (x, i) []
label' i (Tree x ts) =
let
processed = goThrough i ts
Tree (y, label) list = last processed
in
Tree (x, label+1) processed
goThrough :: Int -> [Tree a] -> [Tree (a, Int)]
goThrough _ [] = []
goThrough i (t:ts) =
let
Tree (val, label) list = label' i t
in
[Tree (val, label) list] ++ goThrough (label+1) ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment