I have a list of tuples that look like this.
[ [(1,True),(2,True)] , [(3,False),(4,False),(5,False)] ]
My goal is so get [1,2,3,4,5]
of out that mess.
I tried to use map
and filter
to remove all the Bool
s but filter
also removed the first element in 开发者_StackOverflow中文版the tuple. If I can remove the Bool
s in each tuple then I can maybe use a loop to assign fst
of each tuple to a new empty list?
f :: [[(a, b)]] -> [a]
f = concatMap (map fst)
Then f [ [(1,True),(2,True)] , [(3,False),(4,False),(5,False)] ]
is [1,2,3,4,5]
.
Or with list comprehensions:
f xss = [fst x | xs <- xss, x <- xs]
Try this (edited, thanks to Michael Kohl's and pat's comments):
fun :: [[(a,b)]] -> [a]
fun = map fst . concat
Then in GHC:
*Main> let l = [ [(1,True),(2,True)] , [(3,False),(4,False),(5,False)] ]
[[(1,True),(2,True)],[(3,False),(4,False),(5,False)]]
*Main> fun l
[1,2,3,4,5]
foldl1 (\acc x -> acc ++ x) [a | b <- lst, let a = map fst b]
精彩评论