简体   繁体   English

Haskell列表-计算元组的fst并与列表长度进行比较

[英]Haskell Lists - Count fst of tuples and compare with length of list

I have the following list: 我有以下清单:

[(Libri,50.0),(Proxis,20.0),(Proxis,45.0),(Amazon,45.0)]

And I have a certain List called articles . 而且我有一个称为“ articles列表。 I want to transform the list above, with tuples so that if I have (length articles) times the first element of a tuple showing up in the list, get only that! 我想用元组转换上面的列表,这样,如果我使(元篇文章)乘以元组的第一个元素出现在列表中,就只能得到它!

So, for example, if I have a list articles=["HP","Haskell"] , the list should show: 因此,例如,如果我有一个列表articles=["HP","Haskell"] ,则该列表应显示:

[(Proxis,20.0),(Proxis,45.0)]

Because Proxis shows up two times! 因为Proxis出现了两次!

Edit: 编辑:

Data Types: 资料类型:

data Magasin = Proxis | Amazon | Libri deriving (Eq, Show)
type Article = String
type Prix = Float
data Entree = E Magasin Article Prix deriving (Eq, Show)
type Stock = [Entree]

This is my current code: 这是我当前的代码:

disponible::[Article]->Stock->[(Magasin,Float)]
disponible [] stk = []
disponible (art:reste) stk = (foldl(\acc (E m a p)->if a==art then (m,p):acc else acc) [] stk)++(disponible reste stk)

Any ideas? 有任何想法吗?

Edit: 编辑:

disponible::[Article]->Stock->[(Magasin,Float)]
disponible articles stock = map(\(m,ls)->(m,sum $ map (\(E _ _ p)->p) ls)) $ filter ((==length articles).length.snd) magasinsArticles
     where contientArticles = (filter (\(E _ a _)->a`elem`articles) stock)
           magasins = foldl (\acc e-> if e`elem`acc then acc else (e:acc)) [] $ map (\(E m _ _)->m) contientArticles
           magasinsArticles = map (\m->(m,filter(\(E m2 _ _)->m2==m) contientArticles)) magasins

Found the answer I was looking for. 找到了我想要的答案。

disponible::[Article]->Stock->[(Magasin,Float)]
disponible articles stock = map(\(m,ls)->(m,sum $ map (\(E _ _ p)->p) ls)) $ filter ((==length articles).length.snd) magasinsArticles
     where contientArticles = (filter (\(E _ a _)->a`elem`articles) stock)
           magasins = foldl (\acc e-> if e`elem`acc then acc else (e:acc)) [] $ map (\(E m _ _)->m) contientArticles
           magasinsArticles = map (\m->(m,filter(\(E m2 _ _)->m2==m) contientArticles)) magasins

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

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