In Haskell, equality test is normally performed using == coming the Eq class. This function is (in most cases) defined under pure Haskell terms, so it follows all consequences of applying it recursively to big data structures. Therefore, a seemingly trivial comparison can take significant amount of time. Come on, this should be instant (reminder about laziness):
ghci> let x = [1..100000000] in x == x
True
(2.81 secs, 14,400,130,800 bytes)
Why doesn't Haskell use comparison by reference here? Does Haskell even allow me to do so if I really want to?
xs = [1, 2, 3]andys = id xsand then later I askxs == ys, if==means a pointer comparison then I get different results depending on whetheryshas been evaluated or not. And if that's observable then we need to carefully define when evaluation can happen, so that we can make sure the result of evaluatingxs == ysis predictable. Which would basically force us to being an imperative language with code as a sequence of steps, rather than a "timeless" expression language. Plus it rules out many compiler optimisations.reallyUnsafePtrEquality#and these other questions: stackoverflow.com/q/9649500, stackoverflow.com/q/19355772, stackoverflow.com/q/30175974, stackoverflow.com/q/48163259.