简体   繁体   English

列表列表haskell

[英]lists of lists haskell

I have a simple question, i am new to Haskell and trying to learn a lot. 我有一个简单的问题,我是Haskell的新手,正在尝试学习很多东西。 How can i concatenate for example 我如何连接例如

 [( (0,1) , [1,2,3]) , ((1,2) , [7,8,9]) ,((0,1) , [4,5,6]) ] to [[1,2,3,4,5,6] , [7,8,9]]

I want to concatenate the lists that have first element of the pair equal. 我要串联具有成对的第一个元素的列表。 I tried 我试过了

 map concat list 

 map (\t -> (filter (==).first) list ) list 

but the first one doesn't work and the second one gives an error. 但是第一个不起作用,第二个给出错误。 Any ideas? 有任何想法吗?

using a couple functions from Data.List and Data.Function.on this becomes: 使用Data.ListData.Function.on的几个函数,它变成:

import Data.List     (sortBy, groupBy)
import Data.Function (on)

sortByFirst :: Ord a => [(a, b)] -> [(a, b)]
sortByFirst = sortBy (compare `on` fst)

the groupBy functions expect that the data is sorted, so we must first sort by each inner tuple (ie sort by (a, b) in ((a, b), [x,y,z]) ) groupBy函数期望对数据进行排序,因此我们必须首先按每个内部元组进行排序(即按((a, b), [x,y,z])(a, b)进行排序)

groupByFirst :: Eq a => [(a, b)] -> [[(a, b)]]
groupByFirst = groupBy ((==) `on` fst)

then we use groupBy to group the resulting list of tuples into lists of lists of tuples, where each inner list shares the same first element. 然后我们使用groupBy将结果的元组列表分组为元组列表列表,其中每个内部列表共享相同的第一个元素。

squishByFirst :: (Eq a, Ord a) => [(a, b)] -> [[b]]
squishByFirst = (map . concatMap) snd . groupByFirst . sortByFirst

then we use some fancy mapping over snd to roll into each inner list, pull out the second element (the list), combine them together, and give back the result. 然后我们在snd上使用一些奇特的映射,将其滚动到每个内部列表中,拉出第二个元素(列表),将它们组合在一起,然后返回结果。 This is equivalent to: 这等效于:

squishByFirst xs =
  let grouped = groupByFirst . sortByFirst $ xs
  in  [concat [ys | (_, ys) <- g] | g <- grouped]

You can also implement this as a fold over Map.insertWith 您也可以将其实现为Map.insertWith的折叠

import Data.Map (elems, empty, insertWith)

squishByFirst :: Ord k => [(k, [v])] -> [[v]]
squishByFirst = elems . mkMap
  where
  mkMap = foldr (uncurry (insertWith (++))) empty

or for any Monoid: 或任何Monoid:

squishByFirst :: (Ord k, Monoid vs) => [(k, vs)] -> [vs]
squishByFirst = elems . mkMap
  where
  mkMap = foldr (uncurry (insertWith mappend)) empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM