我尝试将此代码转换为 CPS 表单:
def sum ( lst : List [ Int ]) : Int = lst match {
case Nil => 0
case first :: rest => first + sum ( rest )
}
def sumC1(lst : List [ Int ], k : Int => Unit ) : Unit = lst match {
case lst => k(sum(lst))
}
我是 scala 的新手,在理解语法方面遇到了很大的问题。如果你给我一些语法来解决这个任务会很有帮助
这是我的代码类型不匹配:
def sum(lst: List[Int])(cont: Int => Int): Int = lst match {
case Nil => cont(0)
case first :: rest => sum(lst){rest => cont(first + rest) }
}
def sumC1(lst: List[Int], k: Int => Unit): Unit = lst match {
case lst => k(sum(lst))
}
sumC1(List(1, 2, 3), (v: Int) => println(v))