B.abyface
B.abyface

Reputation: 9

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

Upvotes: 1

Views: 2608

Answers (1)

fp_mora
fp_mora

Reputation: 714

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.

Upvotes: 4

Related Questions