edit: I normally experiment in ghci to understand new code so for those like myself I have created a School of Haskell post/page which comes with the below examples but they are editable and runnable.
Think this examples will answer your questions but for expediency I am going to modify a different node. My knowledge of the zipper functions in the
lens is rather shallow. It takes a little longer to read and get used to the types in the
lens package compared to many other packages, but afterwards it is not bad. I had not used the zipper module or the tree module in the lens package before this post.
The trees do not pretty pretty well with show
so if I have time I will come back and add some pretty printed out put otherwise it is probably key to work in the repl with these examples to see what is happening.
Viewing
If I want to view the value of the first node, according to the tree lens package this is referred to as the root, then you can:
zipperTree & downward root & view focus
Modifying
To modify that value and recreate the tree(rezip the tree):
zipperTree & downward root & focus .~ 10 & rezip
If you wanted to move down the branches then you need to use downward branches
. Here is an example that modifies the root of the first branch and rezipx the tree:
zipperTree & downward branches
& fromWithin traverse
& downward root
& focus .~ 5
& rezip
Here I move downward to the list of branchs. I then use fromWithin
use use traverse
to traverse the list, if this was a tuple I could use both
instead.
Saving and replaying traversal paths
saveTape
and restoreTape
allow for you to save your position in the zipper so that it can be restored latter.
Save a position:
tape = zipperTree & downward branches
& fromWithin traverse
& downward root
& saveTape
Then to recreate the traversal through the tree I can:
t <- (restoreTape tape testTree)
Then you can use t as the new zipper and modify it as normal:
t & focus .~ 15 & rezip
The tape replays the steps that you took so can work on other trees so the follow would work with the tape as defined above:
testTree2 = Node 1 [ Node 2 [] ]
t2 <- (restoreTape tape testTree2)
t2 & focus .~ 25 & rezip
Modifying Multiple locations
If you want to modify multiple roots just hold off on reziping the zipper. The following modifies the two roots of testTree2:
zipper testTree2 & downward root
& focus .~ 11
& upward
& downward branches
& fromWithin traverse
& downward root
& focus .~ 111
& rezip