简体   繁体   中英

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 ?

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.

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.

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