Immutability along with referential transparency leads to a couple of desirable properties. But together they have two drawbacks:
- deep copying
- determination of equality
Both operations are expensive.
The first issue is mitigated by structural sharing. The List
type is one of the simplest persistent data structures and as a result of structural sharing prepending to lists is rather cheap:
xs = [1, 2, 3] -- [1, [2, [3, []]]]
ys = 0 : xs -- [0, [1, [2, [3, []]]]]
But how can I determine the equality between xs
and ys
without checking the equality of each element?
In an imperative language like Javascript I would simply conduct a physical equality check of the root node. Since this invalidates referential transparency, it is not possible in Haskell, of course.
Is there an approach/strategy to avoid the brute force method - at least for some cases?
What I am currently trying in Javascript is to create a equality type that implements the monad class, so that I can determine reference equality without losing RT. But I am too inexperienced to see if this is a promising approach.