[Haskell-beginners] Strange use of undefined (et al.) in list comprehension

Galaxy Being borgauf at gmail.com
Sat Apr 10 03:59:07 UTC 2021


I'm looking at Bird's *Thinking Functionally with Haskell *and he gives two
list comprehensions, asking under what conditions they deliver the same
results

[e | x <- xs, p x, y <- ys]
[e | x <- xs, y <- ys, p x]

First, I'm confused about what is the input and what is the predicate. The y
<- ys in the first LC seems to be in a predicate position, and in the
second it's a second input after x <- xs with p x in the predicate position
. . . confusing me.

The answer examples Bird gives are beyond me:

They deliver the same result only if ys is a finite list:

> [1 | x <- [1,3], even x, y <- undefined]
[]
> [1 | x <- [1,3], y <- undefined, even x]
Exception: Prelude.undefined
> [1 | x <- [1,3], y <- [1..], even x]
{Interruped}

I'm not sure what's being said here, or what points are being made.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210409/37120b98/attachment.html>


More information about the Beginners mailing list