Created
July 4, 2014 11:56
-
-
Save akissinger/8cda224611286dbc9d2b to your computer and use it in GitHub Desktop.
Translated from Huet's "The Zipper"
This file contains hidden or 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
| datatype 'a tree = Item of 'a | Section of 'a tree list | |
| datatype 'a path = Top | Node of 'a tree list * 'a path * 'a tree list | |
| exception zipper_exn of string | |
| fun mk_zipper (t: 'a tree) = (t, Top : 'a path) | |
| fun go_left (_, Top) = raise zipper_exn "cannot go left from Top" | |
| | go_left (t, Node(l::left,up,right)) = (l, Node(left,up,t::right)) | |
| | go_left (_, Node([],_,_)) = raise zipper_exn "cannot go left from leftmost child" | |
| fun go_right (_, Top) = raise zipper_exn "cannot go right from Top" | |
| | go_right (_, Node(_,_,[])) = raise zipper_exn "cannot go right from rightmost child" | |
| | go_right (t, Node(left,up,r::right)) = (r, Node(t::left,up,right)) | |
| fun go_up (t, Top) = raise zipper_exn "cannot go up from Top" | |
| | go_up (t, Node(left,up,right)) = (Section (rev left @ t::right), up) | |
| fun go_down (Item _, _) = raise zipper_exn "cannot go down from Item" | |
| | go_down (Section [], _) = raise zipper_exn "cannot go down from leaf" | |
| | go_down (Section (t1::trees), p) = (t1, Node([],p,trees)) | |
| fun update f (t, p) = (f t, p) | |
| fun set t (_,p) = (t, p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment