[英]error Unit does not take parameters scala
How can I print the element of List after each iteration? 每次迭代后如何打印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))
your call to println(x, xs)
returns a Unit
type, and you're trying to call the return value with argument (1 - x) :: carry(x, xs)
. 您对
println(x, xs)
调用返回的是Unit
类型,并且您尝试使用参数(1 - x) :: carry(x, xs)
调用返回值。 Perhaps you already knew that. 也许您已经知道了。 Curly braces are your friend here, as they create a contained expression.
花括号在这里是您的朋友,因为它们创建了一个包含的表达式。 Here's the solution:
解决方法如下:
case (1, x :: xs) => {println(x, xs); (1 - x) :: carry(x, xs)}
or 要么
case (1, x :: xs) => {
println(x, xs)
(1 - x) :: carry(x, xs)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.