Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.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
fpscala:sequence()函数中的模式匹配错误_Scala - Fatal编程技术网

fpscala:sequence()函数中的模式匹配错误

fpscala:sequence()函数中的模式匹配错误,scala,Scala,我正在学习Scala中的函数编程这本书,并研究问题4.4 我得到一个错误,即使我复制和粘贴的代码从答案键,我不知道如何解决 def sequence[A](a: List[Option[A]]): Option[List[A]] = a match { case Nil => Some(Nil) case h :: t => h.flatMap(hh => sequence(t).map(hh :: _)) } 错误: Error:(45, 12) c

我正在学习Scala中的函数编程这本书,并研究问题4.4

我得到一个错误,即使我复制和粘贴的代码从答案键,我不知道如何解决

  def sequence[A](a: List[Option[A]]): Option[List[A]] = a match {
    case Nil => Some(Nil)
    case h :: t => h.flatMap(hh => sequence(t).map(hh :: _))
  }
错误:

Error:(45, 12) constructor cannot be instantiated to expected type;
 found   : scala.collection.immutable.::[A]
 required: List[Option[?A1]] where type ?A1 <: A (this is a GADT skolem)
    case h :: t => h.flatMap(hh => sequence(t).map(hh :: _))
有什么问题吗

编辑:问题似乎是因为我在同一目录下的不同文件中定义了列表。如何使此文件使用scala.List而不是我定义的列表?

仅隐藏自定义列表类:

从自定义包导入所有其他内容时隐藏列表实现:

或者您也可以这样做,反之亦然-在导入类时为其指定一个新名称或别名:

import scala.collection.immutable.{List => RenamedList}
并使用重命名列表引用scala.List

import custom.{List => _, _}
import scala.collection.immutable.{List => RenamedList}