错误单位未接受参数scala

时间:2018-07-26 20:13:49

标签: scala list

每次迭代后如何打印List的元素?

scala> def carry(c: Int, list: List[Int]):List[Int] = (c, list) match {
     |
     |   case (0, xs) => xs
     |
     |  case (1, Nil) => List(1)
     |
     |  case (1, x :: xs) => println(x,xs)((1 - x) :: carry(x, xs))
     |
     |   case (_, _) => throw new IllegalArgumentException("Invalid input!!!")
     |   }
<console>:18: error: Unit does not take parameters
        case (1, x :: xs) => println(x,xs)((1 - x) :: carry(x, xs))

1 个答案:

答案 0 :(得分:3)

您对println(x, xs)的调用返回了Unit类型,而您试图用参数(1 - x) :: carry(x, xs)调用返回值。也许您已经知道了。花括号在这里是您的朋友,因为它们创建了一个包含的表达式。解决方法如下:

case (1, x :: xs) => {println(x, xs); (1 - x) :: carry(x, xs)}

case (1, x :: xs) => {
  println(x, xs)
  (1 - x) :: carry(x, xs)
}