Last active
December 20, 2015 04:59
-
-
Save bpatra/6074560 to your computer and use it in GitHub Desktop.
Some material for abstracting the tree zipper and an example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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