Ok, I'm trying to make a function to determine if a list of tuples is transitive, i.e. if the list contains (x, y) and (y, z), then (x, z) is also in the list.
For example, [(1,2), (2,3), (1,3)]
is transitive.
Now, based on the Prolog background, the following makes sense to me:
transitive xs = and [elem (x, z) xs | (x, y) <- xs , (y, z) <- xs ]
HOWEVER, it does not work. It seems that "y" does not get a single meaning, as I expected, but "reassigned" when it comes to the second tuple. Instead, we should use:
transitive xs = and [elem (x, z) xs | (x, y1) <- xs , (y2, z) <- xs, y1 == y2 ]
Why is this so? Why does the first example not cause an error and does the principle of functional programming languages โโcontradict "referential transparency?"
"However, in pure functional and logical languages, variables are bound to expressions and retain the same value throughout their lives due to referential transparency requirements." - Wikipedia
Thanks!