Ok, I'm attempting to make a function to determine if a list of tuples is transitive, i.e. if (x,y) and (y,z) is in the list, then (x,z) is also in the list.
For example, [(1,2), (2,3), (1,3)]
is transitive.
Now, coming from a Prolog background, the following makes sense to me:
transitive xs = and [elem (x, z) xs | (x, y) <- xs , (y, z) <- xs ]
HOWEVER, it doesn't work. It appears that the 'y' does not get the single value as I would expect, but is 'reassigned' when it comes to the second tuple. Instead, we must 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 doesn't this go against functional programming languages' principle of 'referential transparency?'
"However, in pure functional and logic languages, variables are bound to expressions and keep a single value during their entire lifetime due to the requirements of referential transparency." - Wikipedia
Thanks!
List comprehension in Haskell is a way to produce the list of new elements from the generator we have passed inside it. Also for the generator values, we can apply the Haskell functions to modify it later. This list comprehension is very y easy to use and handle for developers and beginners as well.
List comprehension is an elegant way to define and create lists based on existing lists. List comprehension is generally more compact and faster than normal functions and loops for creating list.
Yes, the list comprehension preserves the order of the original iterable (if there is one). If the original iterable is ordered (list, tuple, file, etc.), that's the order you'll get in the result. If your iterable is unordered (set, dict, etc.), there are no guarantees about the order of the items.
Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.
Even in functional languages, there is name shadowing. Sometimes that is useful.
In the first code, the (y,z) <- xs
shadows the y
bound by the (x,y) <- xs
before.
Compile with warnings turned on to be alerted of such things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With