Skip to content

Instantly share code, notes, and snippets.

@bpatra
Last active December 20, 2015 04:59
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 bpatra/6074560 to your computer and use it in GitHub Desktop.
Save bpatra/6074560 to your computer and use it in GitHub Desktop.
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