Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Strange use of undefined (et al.) in list comprehension (Galaxy Being) 2. Re: Strange use of undefined (et al.) in list comprehension (Matthew Low) ---------------------------------------------------------------------- Message: 1 Date: Fri, 9 Apr 2021 22:59:07 -0500 From: Galaxy Being <borg...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Strange use of undefined (et al.) in list comprehension Message-ID: <cafahfsxrepaduwwjzhuyd+sb0ypsl5kf4zcvehzhhks6mfm...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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-0001.html> ------------------------------ Message: 2 Date: Fri, 9 Apr 2021 23:17:20 -0600 From: Matthew Low <m...@ualberta.ca> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Strange use of undefined (et al.) in list comprehension Message-ID: <CAC=gTKwwZvw+dYG_c=ZGbZ4v2aAaWLtE4PkAWvdVw62e=6w...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" > > First, I'm confused about what is the input and what is the predicate The haskell 2010 report ( https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-420003.11) says that after the | we can have any type of qualifier, which can be 1. a generator (I think you called this the input) 2. a local binding (we don't care about these in your example, there are none) or 3. boolean guards, which are any expression that evaluate to Bool (your predicates). So there isn't really a `predicate postion`, predicates can occur anywhere after the `|`. As for the behaviour, its easiest to see what happens with a regular list of inputs: λ> [(x,y) | x <- [1,2], y <- ['A', 'B']] [(1,'A'),(1,'B'),(2,'A'),(2,'B')] so `y` acts as an inner loop and `x` the outer. > [1 | x <- [1,3], even x, y <- undefined] > Here we start the outer loop over x, and only if x is even, then we loop over y. But x is never even, so we never loop over y, so we never evaluate `undefined` (Haskell is lazy). So essentially filter out all elements of x and are left with [] > [1 | x <- [1,3], y <- undefined, even x] > Now we've moved the guard into the inner loop, after we try to evaluate y. So we blow up trying to do that and GHCi catches the exception [1 | x <- [1,3], y <- [1..], even x] > Similar to the above, the (even x) isn't guarding the evaluation of y, so we're stuck generating all the infinite pairings of x = 1, y = 1... On Fri, Apr 9, 2021 at 9:59 PM Galaxy Being <borg...@gmail.com> wrote: > 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. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20210409/5717f0aa/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 153, Issue 3 *****************************************