[英]Haskell “concat” list of tuples using only list comprehensions
How to change of list of tuples, like 如何更改元组列表,例如
[(5,6),(7,8),(9,10)]
into a normal list, like 进入正常列表,例如
[5,6,7,8,9,10]
via list comprehensions and without concat
? 通过列表concat
和没有concat
吗?
I have tried this: 我已经试过了:
[ [y, z] | xs <- [(1,2),(3,4)], y <- fst(xs), z <- snd(xs) ]
To flatten any list with a list comprehension, the form is always the same. 为了用列表理解来平整任何列表,形式总是相同的。 Take the multiple elements from the source one-at-a-time. 一次从源中获取多个元素。
List comprehensions, like functions let you specify and exact pattern of the source, tuple or list. 列表推导(如函数)可让您指定源,元组或列表的确切模式。
Your function is not in the form of multiples, one-at-a-time, so correcting it will never give you what you want. 您的函数不是一次一次的倍数形式,因此更正它永远不会给您您想要的。 It will, at very least require the use of concat
to concatenate the output. 至少将需要使用concat
来连接输出。
Here is the form of a flattening list comprehension 这是扁平化列表理解的形式
[ n |(a,b)<-[(1,2),(3,4),(5,6)],n <-[a,b]]
a
and b
are taken one-at-a-time by n
, to flatten. a
和b
一次被n
取整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.