我需要从这里开始:
[(2,"a"), (1,"a"), (1,"b"), (1,"c"), (2,"dd")]
到这里:
[([1, 2], "a"), ([1], "b"), ([1], "c"), ([2], "dd")]
到目前为止我已经
了combineInts listTuple = someFunc (map (\(num, str) -> ([num], str)) listTuple)
其中" someFunc"我还需要弄清楚并实施。我相信它应该利用foldr,map和/或intercalate来实现我的目标。有什么想法吗?
答案 0 :(得分:5)
这应该做:
import Data.Function (on)
import Data.List (groupBy, sort, sortBy)
out = map f . groupBy ((==) `on` snd) . sortBy (compare `on` snd) $ input
where input = [(2,"a"),(1,"a"),(1,"b"),(1,"c"),(2,"dd")]
f xs@(x:_) = (sort $ map fst xs, snd x)
main = print out
答案 1 :(得分:2)
如果您先订购列表,则可以使用groupBy
执行此操作:
import Data.List (groupBy)
import Data.Ord (comparing)
import Data.Function (on)
map (\l@((_,c):_) -> (map fst l, c)) $ groupBy (on (==) snd) $ sortBy (comparing snd) inputList