简体   繁体   中英

Haskell Nested List Comprehensions

I am studying for an exam and I am looking at an example of nested list comprehensions from the book "Learn you a Haskell", and I was hoping if anyone could explain me step by step how to analyze it and come out with its output.

let xxs = [[1,2,3],[2,3,4],[4,5]]

[ [ x | x <- xs, even x] | xs <- xxs ]] 

Output: ([[2],[2,4],[4]])

[ [ x | x <- xs, even x] | xs <- xxs ]
[ [ x | x <- xs, even x] | xs <- [[1,2,3],[2,3,4],[4,5]] ]
[ [ x | x <- [1,2,3], even x] , [ x | x <- [2,3,4], even x] , [ x | x <- [4,5], even x] ]
[filter even [1,2,3], filter even [2,3,4], filter even [4,5]]
[[2],[2,4],[4]]

Or

[ [ x | x <- xs, even x] | xs <- xxs ]
map (\xs -> [ x | x <- xs, even x] ) xxs
map (\xs -> filter even xs) [[1,2,3],[2,3,4],[4,5]]
[filter even [1,2,3], filter even [2,3,4], filter even [4,5]]
[[2],[2,4],[4]]

Note this isn't the transformation that GHC actually does, just a way of writing it that might help you understand the output.

List comprehensions could have been defined by few identities:

[ f x | x <- [],  ... ]       ===   []
[ f x | x <- [y], ... ]       ===   [ f y | {y/x}... ]   -- well, actually, it's
                                    -- case y of x -> [ f y | {y/x}... ] ; _ -> []

[ f x | x <- xs ++ ys, ...]   ===   [ f x | x <- xs, ...] ++ [ f x | x <- ys, ...]

[ f x | True, ...]            ===   [ f x | ... ]
[ f x | False, ...]           ===   []

The handling of complex patterns (as opposed to simple variable patterns) is elided, only hinted at, for simplicity. {y/x}... means, y is substituted for x in ... . For actual definition, see the Report .

It follows that

[ f x | xs <- xss, x <- xs]   ===  concat [ [f x | x <- xs] | xs <- xss] 

and

[ f x | x <- xs, test x ]     ===  map f (filter test xs)

Your expression is equivalent to

[ [ x | x <- xs, even x] | xs <- xxs ]   -- `]`, sic!
=
[ f xs | xs <- xxs ]  where  f xs = [ x | x <- xs, even x]

Which is to say, there's nothing special about a list comprehension being used as a value expression in the definition of f . It looks "nested", but actually, it isn't.

What is nested, are the generator expressions separated by commas:

[ x | xs <- xss, x <- xs ]   ===   concat [ [x | x <- xs] | xs <- xss ]
--              ^^^ nested generator

(the equivalency like we saw above.) So then,

[ [ x | x <- xs, even x] | xs <- [[1,2,3],[2,3,4],[4,5]] ]
=
[ [ x | x <- [1,2,3], even x]] ++ [[ x | x <- [2,3,4], even x]] ++ [[ x | x <- [4,5], even x] ]
=
[ [ x | x <- [1,2,3], even x], [ x | x <- [2,3,4], even x], [ x | x <- [4,5], even x] ]
=
[ [ x | x <- [1], even x]++[ x | x <- [2], even x]++[ x | x <- [3], even x]
, [ x | x <- [2], even x]++[ x | x <- [3], even x]++[ x | x <- [4], even x]
, [ x | x <- [4], even x]++[ x | x <- [5], even x] ]
=
[ [ 1 | even 1]++[ 2 | even 2]++[ 3 | even 3]
, [ 2 | even 2]++[ 3 | even 3]++[ 4 | even 4]
, [ 4 | even 4]++[ 5 | even 5] ]
=
[ []++[ 2 ]++[], [ 2 ]++[]++[ 4 ], [ 4 ]++[] ]
=
[ [2], [2,4], [4] ]

Or, with filter if you'd prefer,

[ [ x | x <- [1,2,3], even x], [ x | x <- [2,3,4], even x], [ x | x <- [4,5], even x] ]
=
[ filter even [1,2,3], filter even [2,3,4], filter even [4,5] ]
=
[ [2], [2,4], [4] ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM