Skip to content

Instantly share code, notes, and snippets.

@bpatra bpatra/gist:6074560
Last active Dec 20, 2015

Embed
What would you like to do?
Some material for abstracting the tree zipper and an example
//Get the root tree from a zipper
let rec root (Loc (t, p) as l) =
match p with
| Top(_) -> t
| _ -> root (go_up l)
//creates zipper from a tree
let getZipper t =
match t with
| Empty -> Loc(Empty, Top(""))
| TreeNode(value,_ )-> Loc(t, Top(value))
//this is the function to use, the zippers are abstracted.
let appendToTree t (branch:list<string>) =
appendToTreeZipper <| getZipper t <| branch
|> root
let branch1 = ["a";"b";"c"]
let tree1 = appendToTree Tree.Empty branch1
let branch2 = ["a";"b";"d"]
let tree2 = appendToTree tree1 branch2
let branch3 = ["a";"f"]
let final = appendToTree tree2 branch3
//final corresponds to the following tree
// a
// / \
// b f
// / \
// c d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.