简体   繁体   English

Haskell嵌套列表理解

[英]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]]) 输出: ([[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. 请注意,这不是GHC实际执行的转换,只是一种可以帮助您理解输出的编写方式。

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 ... . {y/x}...手段, y代替x... 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 . 也就是说,列表推导在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, 或者,如果您愿意,可以使用filter

[ [ 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] ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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