则从列表中删除项目

则从列表中删除项目

本文介绍了如果谓词成立,则从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个函数,把一个谓词和一个列表作为输入。并从谓词所保存的列表中删除所有元素。到目前为止,我的功能如下:

  removeif :: func-> [a]  - > [a] 
removeif [] = []
removeif func(h:t)= if func then delete h(h:t)else removeif func t

我对 func-> [a]中的 func 部分感到困惑, - > [a] 因为我不知道该如何判断它是一个谓词。



例如,我想要的是我给终端这个命令

  removeif threefolds [1,2,3,4,5,6,7,8, 9] 

threefolds 是一个局部函数I有,它返回一个无限列表的三倍数,我想从输入列表中删除这些倍数。我正在考虑让三倍一个bool函数,但不确定是否有必要。

解决方案 div>

谓词应该是 a - >>类型的函数。布尔。因此, removeif 的签名应该是

  removeif ::(a - > Bool) - > [a]  - > [a] 

如果 threefolds code> a - > Bool ,你会没事的: - )


I am trying to make a function which takes as input a predicate and a list. and removes all elements from the list for which the predicate holds. What I have so far is the following function:

removeif :: func->[a]->[a]
removeif [] = []
removeif func (h:t)= if func then delete h (h:t) else removeif func t

I am confused about the func part of the func->[a]->[a] because I don't know how should I tell that its a predicate.

For example what I want is that I give from the terminal this command

removeif threefolds [1,2,3,4,5,6,7,8,9]

threefolds is a local function I have and it returns an infinite list with multiples of three and I want to remove those multiples from the input list. I am thinking of making threefold a bool function but not sure if that is necessary.

解决方案

A predicate should be a function of type a -> Bool. Therefore, the signature of removeif should be

removeif :: (a -> Bool) -> [a] -> [a]

If threefolds has type a -> Bool, you'll be fine :-)

这篇关于如果谓词成立,则从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:56