Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'unification' in list comprehensions

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!

like image 222
Jarmex Avatar asked Apr 16 '12 17:04

Jarmex


People also ask

What are list comprehensions in Haskell?

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.

What are list comprehensions in Python?

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.

Are list comprehensions ordered?

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.

Are list comprehensions faster than for loops?

Because of differences in how Python implements for loops and list comprehension, list comprehensions are almost always faster than for loops when performing operations.


1 Answers

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.

like image 147
Daniel Fischer Avatar answered Oct 13 '22 00:10

Daniel Fischer