I have two rose trees in Haskell of m and n nodes, respectively. I want to replace the ith node of the first tree with the jth node of the second tree.
e.g.
tree 1:
R
_________|____________
| | |
A B C
/ \ / \ / \
D E F G H I
tree 2:
r
_____|____
| |
P Q
/ \ / | \
S T U V W
then the resulting tree of tree 1 node 7 (C) replaced with tree 2 node 4 (Q) should be (assuming indexing is pre order and starting at 0)
R
_________|____________
| | |
A B Q
/ \ / \ / | \
D E F G U V W
I have tried using a zipper, but my problem is I can't workout how to get the zipper focus to the ith element,
i.e. how can i implement a function with type:
someFunc :: Tree a -> Int -> Zipper a
that take the root of the tree and traverses it (in some order) to return the zipper focussed on the ith pre order node and the context.
from the tree library I can flatten a tree to a pre order list of values using flatten, i can change this slightly to give me a list of trees e.g.
flattenToTreeList :: Tree a -> [Tree a]
flattenToTreeList t = squish t []
where squish (Node x ts) xs = Node x ts : foldr squish xs ts
if I could do this but with a zipper then the ith element of this list would satisfy me, but I'm lost and now going round in circles.