根据Haskell列表中的n的条件移除元素n-1,n和n + 1 [英] Remove elements n-1, n and n+1 based on a condition for n in a Haskell list

查看:145
本文介绍了根据Haskell列表中的n的条件移除元素n-1,n和n + 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以说我有一个从 2 20

  [2..20] 



<我想通过使用函数 fx 来筛选整个列表(或者它是一个谓词吗?我不是很习惯Haskell编程中使用的所有术语)。如果 n 位置的元素对于这个函数 f 等于true,我想删除位置 n-1 n n + 1



例如:
假设列表 [2 .. 20] 中位置4的元素,等于 6 ,等于函数 f 的真。然后我想删除位置 3 4 5 5 , 6 7
因此,我的最终名单如下所示:

  [2,3,4,8,9,10,11 ,12,13,14,15,16,17,18,19,20] 

我是一个没有经验的Haskell程序员,只是为了好玩而玩。我曾经想过使用lambda函数作为谓词,但我不太确定如何去做。我也考虑过使用像 remove xs ys 这样的函数,它删除 xs 中的所有元素,这也是元素 ys ,但我不确定如何做到这一点。



任何帮助将不胜感激!编辑:我意识到,删除两个相邻的元素是错误的,以产生我想要的结果。另外,改变受影响的元素(位置 n n-1 )到 0 ,或以其他方式标记/标记它们,而不是完全删除它们。原因是我想保留去除元素,直到列表中不再有符合谓词的元素(及其前面的元素)。我只想将其从原始列表中移除。
由于我的方法与原来的问题有很大的变化,所以我会发一个新的方法来反映我的新方法。我想感谢所有的答复,我从你的答案中学到了很多东西。谢谢!



编辑2:这是我的新问题:

您可以对多个元素进行模式匹配,并将过滤器应用于中间元素。 b
$ b

  eitherside ::(Int-> Bool) - > [Int]  - > (int)
eitherside f(i1:i2:i3:is)= if(f i2)
then eitherside f is
else i1:(eitherside f(i2:i3:is))
eitherside f is = is
* Main> eitherside(== 4)[1..10]
[1,2,6,7,8,9,10]
* Main> eitherside(== 5)[1..10]
[1,2,3,7,8,9,10]
* Main> eitherside(== 6)[1..10]
[1,2,3,4,8,9,10]

不像这样(我原来的帖子):

  eitherside ::(Int-> ; Bool) - > [Int]  - > (i1:i2:i3:is)= if(f i2)
then eitherside f is
else [i1,i2,i3] ++(eitherside f is )
eitherside f is = is
* Main> eitherside(== 5)[1..10]
[1,2,3,7,8,9,10]

这个坏的发生在5,但是因为我在else分支中跳过了6而失败了。


So say I have a list of all integers from 2 to 20.

[2 .. 20]

I want to filter through the list using a function f x (or is it a predicate? I'm not very used to all the terms used in Haskell programming). If the element at position n equals to true for this function f, I want to remove the elements at positions n-1, n and n+1.

Example: Let's say that the element at position 4 in the list [2 .. 20], which equals to 6, equals to true for the function f. Then I want to remove the elements at position 3, 4 and 5, which equal to 5, 6 and 7 respectively. So my final list would look like:

[2,3,4,8,9,10,11,12,13,14,15,16,17,18,19,20]

I am an inexperienced Haskell programmer, just playing around for fun. I have thought about using a lambda function as predicate but I am not quite sure how to go about it. I have also thought about using a function like remove xs ys that removes all elements in xs that is also a element of ys, but I'm not sure how to do that either.

Any help would be appreciated!

EDIT: I realized that removing both adjacent elements is wrong in order to produce the result I wanted. Additionally, it might be better to just change the value of the affected elements (elements at positions n and n-1) to 0, or tag/mark them in some other way, instead of removing them entirely. The reason for that is that I want to keep "removing" elements until the list no longer has any elements that fit the predicate (and their preceding elements). I only want to "remove" them from the original list. Since my approach has changed so much from the original question, I will post a new one with to reflect my new approach. I want to thank you for all the responses and I have learned a lot from your answers. Thank you!

EDIT 2: Here is my new question: Remove elements at positions n and n-1 in a Haskell list, when n fits a predicate

解决方案

You can just pattern-match on multiple elements and apply your filter to the middle one.

eitherside :: (Int->Bool) -> [Int] -> [Int]
eitherside f (i1:i2:i3:is) = if (f i2) 
    then eitherside f is 
    else i1 : (eitherside f (i2:i3:is))
eitherside f is = is
*Main> eitherside (==4) [1..10]
[1,2,6,7,8,9,10]
*Main> eitherside (==5) [1..10]
[1,2,3,7,8,9,10]
*Main> eitherside (==6) [1..10]
[1,2,3,4,8,9,10]

NOT LIKE THIS (my original post):

eitherside :: (Int->Bool) -> [Int] -> [Int]
eitherside f (i1:i2:i3:is) = if (f i2) 
    then eitherside f is 
    else [i1,i2,i3] ++ (eitherside f is)
eitherside f is = is
*Main> eitherside (==5) [1..10]
[1,2,3,7,8,9,10]

This bad one happened to work for 5, but fails for 6 because I'm skipping it in the "else" branch.

这篇关于根据Haskell列表中的n的条件移除元素n-1,n和n + 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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