我正在学习 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
and last
工作,但为了功能concat
我收到错误:
Error:(13, 9) not found: value concat
concat(xs, ys) == x
Why is concat
未找到?
请注意,如果我使用 Scala 控制台sbt-shell
并执行相同的命令,然后一切正常。
如何配置 IntelliJ 以毫无问题地使用工作表?