【发布时间】:2019-05-19 08:31:07
【问题描述】:
我正在学习 Scala 中的函数式编程原理课程 但是我在 IntelliJ 中使用 Scala Worksheets 进行快速测试时遇到了很多问题。
例如,我建立了一个新的 Scala 项目,并在其中创建了一个名为 lecture5 的包对象(在文件中)src/main/scala/lecture5/package.scala
文件内容为:
package object lecture5 {
def last[T](xs:List[T]): T = xs match {
case List() => throw new Error("empty list")
case List(x) => x
case x :: y => last(y)
}
/* init should return all elements but last */
def init[T](xs: List[T]): List[T] = xs match {
case List() => throw new Error("List is empty")
case List(x) => List[T]()
case y :: ys => y :: init(ys)
}
def concat[T](xs: List[T], ys: List[T]): List[T] = xs match {
case List() => ys
case z:: zs => z :: concat(zs, ys)
}
}
在工作表中我有以下内容:
import lecture5._
val x = List("a","b","c")
val xs = List("a","b")
val ys = List("c")
last(x)
init(x)
concat(xs, ys) == x
在工作表的设置中,我检查了Interactive Mode、Make project before run 并使用了Run Type = REPL(Plain 由于某种原因不起作用)和Compiler profile = Default。
当我单击“play”按钮运行工作表时,函数 init 和 last 工作,但函数 concat 出现错误:
Error:(13, 9) not found: value concat
concat(xs, ys) == x
为什么找不到concat?
请注意,如果我在 sbt-shell 中使用 Scala 控制台并执行相同的命令,那么一切正常。
如何将 IntelliJ 配置为使用工作表而不会出现问题?
【问题讨论】:
标签: scala intellij-idea