简体   繁体   English

Haskell数据类型过滤子列表列表

[英]Haskell List of Data Type Filter Sub List

I got a person data type that has a name and their weight looks like below. 我有一个人名数据,其名称和重量如下所示。

data Person = Person
    {pName :: String,
     pWeight :: Double} deriving (Show)

persons :: [Person]
persons = [Person "Peter" 35.0,
            Person "Joe" 45.0,
            Person "Alex" 55.0,
            Person "Ruby" 60.0,
            Person "John" 65.0,
            Person "Ann" 35.0]

Q1 If I want to filter and get the first sub set from the given persons list that their maximum weights sum is to be less than a given value how to do it? Q1如果我想过滤并从给定人员列表中获取第一个子集,他们的最大权重总和应小于给定值怎么做?

eg Output if maximum Weight is 150.0 例如,如果最大重量为150.0,则输出

Person "Peter" 35.0 人“彼得”35.0

Person "Joe" 45.0 人“乔”45.0

Person "Alex" 55.0 人“亚历克斯”55.0

Total weight : 135.0 总重量:135.0

Q2 If I want to get all the subsets of a maximum weight that is set Q2如果我想获得所设置的最大权重的所有子集

eg Output if max weight is 100.0 例如,如果最大重量为100.0,则输出

Set 1 设置1

Person "Peter" 35.0 Person "Joe" 45.0 人“彼得”35.0人“乔”45.0

Total weight : 80.0 总重量:80.0

Set 2 设置2

Person "Alex" 55.0 人“亚历克斯”55.0

Total weight : 55.0 总重量:55.0

Set 3 ...... so on 设置3 ......等等

The first can be done using a filter. 第一种可以使用过滤器完成。 The second seems like a knapsack problem. 第二个似乎是一个背包问题。 In which case you can search for strategies to solve it. 在这种情况下,您可以搜索解决它的策略。 If you run into any issues after you write your code, please re-post here and the community will be glad to help. 如果您在编写代码后遇到任何问题,请在此处重新发布,社区将很乐意为您提供帮助。

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

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