Moreover, copying is not even meaningful in a functional setting. A data structure is indistinguishable from a copy of the data structure. In languages that allow mutation of data, one has to carefully copy data to avoid accidental mutation by other computations. Disallow data mutation, and the problem disappears.
- Conal
Rouan van Dalen wrote:Others have already recommended the rosezipper package, which gives you what you want, but I want to address one thing.
It is important to store only a reference to the parent and not a copy of the entire parent for efficiency.
foo = <stuff>
bar = foo
In most implementations (including GHC, which I assume you are using), `bar` will *not* be an actual copy of `foo`. In fact, GHC will not make a deep copy of a structure unless you pattern match all the way down and reconstruct it all the way back up yourself.
If you use a zipper, like Data.Tree.Zipper mentioned already, moving around will not create and destroy tons of data. It will only create and destroy the spine of the tree local to the current "pointer" into the tree, which is not a lot of data. If you are holding on to older versions of the zipper, of course, they won't even be destroyed.
- Jake
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe