I'm currently studying for my exam in October and am confronting a problem I can't seem to figure out a good solution.
I want to read in a list of tuples of integers like this: [(1,2),(3,4),(5,6),..] and want the list comprehension to return [1,2,3,4,5,6,..]
The following works just fine but I want it to be in one list comprehension.
func :: [(Integer, Integer)] -> [Integer]
func xs = concat [ [x,y] | (x,y) <- xs ]
How can I make this work?
答案 0 :(得分:7)
You can use an extra iteration in the right part of the list comprehension, like:
func :: [(a, a)] -> [a]
func xs = [ xi | (x1, x2) <- xs, xi <- [x1, x2] ]
So we write list comprehension like people would write a nested loop in an imperative programming language.