Haskell使用数据构造函数过滤嵌套列表 [英] Haskell filtering a nested list with data constructors

查看:112
本文介绍了Haskell使用数据构造函数过滤嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据类型:

  data数字=正整数|负整数
导出(Eq,Show)

我有一个函数定义(注意THAT我不能改变这个定义,或者以任何方式工作,我必须使用嵌套列表并以某种方式进行修改)

  removePos :: [[Number]]  - > [[Number]] 

所以我们有一个嵌套的Number列表。一个例子是

  [[Positive 1,Negative 1],[Positive 2,Negative 2,Positive 1],[Positive 1]] 

如何编写removePos以删除嵌套列表中包含Positive x,其中x是特定整数?该函数实质上是查看第一个列表中的第一个元素,如果它是Positive,那么删除包含Positive x的所有列表。



基本上,如果我们看一下在上面的例子中,输出将是

  [[]] 
$ b

请注意,用户执行以下函数调用:

  removePos [ [Positive 1,Negative 1],[Positive 2,Negative 2,Positive 1],[Positive 1]] 



由于上面嵌套列表中的每个列表都包含Positive 1,所以输出只是一个空的嵌套列表(所有带有Positive x的列表都将被删除)。然而,如果第一个列表中的第一个元素是正数10,那么输出将是

  [[Positive 2,Negative 2,正面1],[正面1]] 

(因为第一个列表会有[Positive 10,Negative 1

编辑:



任何想法? b

有关其他示例,可以说我有一个装有水果碗的篮子。篮子是嵌套列表,碗是其中的列表。现在,我看看篮筐。我检查了第一个碗。我看着碗里的第一个水果,并确定我不想吃任何碗里的水果。所以我扔掉所有含有这种水果的碗,并给你回篮子。

嵌套列表来查看第一个列表的第一个元素,以找出需要过滤的内容:

   - 请注意,您可以给这个更一般的类型公式a => [[a]]  - > [[a]] 
- (以及更合适的名称)
removePos :: [[Number]] - > [[Number]]
removePos [] = [] - 空列表大小写。
removePos xss @ [[]:_] = xss - 如果第一个内部列表为空,
- 返回整个事物不变
removePos((x:_):xss) = filter(notElem x)xss


Let's say I have the following data type

data Number = Positive Integer | Negative Integer
     deriving (Eq, Show)

I have a function definition of (NOTE THAT I CANNOT CHANGE THIS DEFINITION, OR WORK AROUND IT IN ANY WAY. I have to work with the nested list and modify it in some way)

removePos :: [[Number]] -> [[Number]]

So we have a nested list of Number. An example would be

[[Positive 1, Negative 1], [Positive 2, Negative 2, Positive 1], [Positive 1]]

How can I write removePos so that it removes all lists in the nested list that contain Positive x, where x is A SPECIFIC Integer? The function is essentially looking at the first element in the first list, if it is Positive then remove all lists that contain Positive x.

Essentially, if we took a look at the example above, the output would be

[[]]

Note that the user performs the following function call

removePos [[Positive 1, Negative 1], [Positive 2, Negative 2, Positive 1], [Positive 1]]

Since each list in the nested list above contains Positive 1, the output is simply an empty nested list (All lists with Positive x are removed). However, if the first element in the first list was Positive 10, the output would be

[[Positive 2, Negative 2, Positive 1], [Positive 1]]

(Because the first list would have [Positive 10, Negative 1], which would get removed)

Any ideas?

EDIT:

For further examples, lets say I have a basket with bowls of fruits in it. The basket is the nested list, the bowls are lists within it. Now, I take a look in the basket. I check the first bowl. I look at the first fruit in the bowl, and determine that I don't want to eat that fruit from any of the bowls. So I throw out all bowls that contain that fruit, and give you back your basket.

解决方案

One way is pattern matching on the nested list to peek at the first element of the first list to figure out what you need to filter:

-- Note you could give this the more general type Eq a => [[a]] -> [[a]]
-- (as well as a more appropriate name)
removePos :: [[Number]] -> [[Number]]
removePos [] = [] -- Empty list case.
removePos xss@[[]:_] = xss -- If the first inner list is empty,
                           -- return the whole thing unchanged
removePos ((x:_):xss) = filter (notElem x) xss

这篇关于Haskell使用数据构造函数过滤嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆