Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 函数确定列表中的成员身份_List_Scala_Element - Fatal编程技术网

List 函数确定列表中的成员身份

List 函数确定列表中的成员身份,list,scala,element,List,Scala,Element,我需要写一个包含函数的程序 doesExist:[A](p:A=>Boolean)(xs:List[A])Boolean e、 g.doesExist((x:Int)=>x==2)(列表(5,1,2,3))=>true 它应该只返回true或false,这取决于是否在列表中找到该元素。简单地说: def doesExist[A](p:A => Boolean)(xs: List[A]) = xs.exists(p) 如果无法使用存在: def doesExist[A](p:A =>

我需要写一个包含函数的程序
doesExist:[A](p:A=>Boolean)(xs:List[A])Boolean

e、 g.
doesExist((x:Int)=>x==2)(列表(5,1,2,3))=>true

它应该只返回true或false,这取决于是否在列表中找到该元素。

简单地说:

def doesExist[A](p:A => Boolean)(xs: List[A]) = xs.exists(p)
如果无法使用
存在

def doesExist[A](p:A => Boolean)(xs: List[A]) =
  xs.fold(false)((alreadyFound, a) => alreadyFound || p(a))
简单地说:

def doesExist[A](p:A => Boolean)(xs: List[A]) = xs.exists(p)
如果无法使用
存在

def doesExist[A](p:A => Boolean)(xs: List[A]) =
  xs.fold(false)((alreadyFound, a) => alreadyFound || p(a))

我想它的要点是不要使用列表的内置功能。。。因此,这里有一个使用
foldLeft
的替代方法:

def doesExist[A](p: A => Boolean)(xs: List[A]): Boolean = {
    xs.foldLeft(false)((acc, elem) => acc || p(elem))
}

我想它的要点是不要使用列表的内置功能。。。因此,这里有一个使用
foldLeft
的替代方法:

def doesExist[A](p: A => Boolean)(xs: List[A]): Boolean = {
    xs.foldLeft(false)((acc, elem) => acc || p(elem))
}

练习模式匹配可能是一个很好的练习:

def doesExist[A](p:A => Boolean)(xs: List[A]): Boolean = xs match {
    case Nil => false
    case head :: tail => p(head) || doesExist(p)(tail)
}

练习模式匹配可能是一个很好的练习:

def doesExist[A](p:A => Boolean)(xs: List[A]): Boolean = xs match {
    case Nil => false
    case head :: tail => p(head) || doesExist(p)(tail)
}

下面是Scala 2.11.8标准库的工作方式

发件人:

override/*IterableLike*/
def存在(p:A=>Boolean):Boolean={

var this=this/以下是Scala 2.11.8标准库的工作方式

发件人:

override/*IterableLike*/
def存在(p:A=>Boolean):Boolean={

var this=this//你的意思是你需要实现这个函数吗?请添加到目前为止你尝试了什么,遇到了什么问题的信息..def doesExist[A](p:A=>Boolean)(xs:List[A]):Boolean={xs.foldLeft(false)((acc,elem)=>acc|p(elem))}编译完成后,现在我试着测试一些数据,尝试这样的形式:doesExist(2)(List(1,2,3))没有正确的回答。解决了这个问题,这样的形式:doesExist((x:Int)=>x==2)(List(1,2,3))返回:doesExist((x:Int)=>x==0)(List(1,2,3))res11:Boolean=false所以我想现在已经很清楚了。你的意思是你需要实现这个函数吗?请添加到目前为止你尝试了什么,遇到了什么问题的信息..def doesExist[A](p:A=>Boolean)(xs:List[A]):Boolean={xs xs.foldLeft(false)((acc,elem)=>acc|p(elem))}编译完成后,现在我试着测试一些数据,尝试这样的形式:doesExist(2)(List(1,2,3))没有正确的回答。解决了这个问题,这样的形式:doesExist((x:Int)=>x==2)(List(1,2,3))返回:doesExist((x:Int)=>x==0)(List(1,2,3))res11:Boolean=false,所以我认为现在已经很清楚了。即使在找到真元素后,它也会遍历整个列表。即使在找到真元素后,它也会遍历整个列表。第二个版本即使在找到真元素后也会搜索整个列表。第一个版本不会。第二个版本即使在ter找到一个真正的元素。第一个版本没有。