-2
我試圖重構一些代碼並使用更高階的函數。但由於某種原因,當我將該函數作爲參數傳遞給它自己的函數時。我收到錯誤無法解析引用「權重」與這樣的簽名。在斯卡拉:如何遞歸地將函數作爲參數傳遞給它自己的函數調用
這裏是我的代碼:
abstract class CodeTree
case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
case class Leaf(char: Char, weight: Int) extends CodeTree
def walk[T](t: CodeTree, op: CodeTree => T, cmb: (T,T) => T) = t match {
case Fork(l,r,_,_) => cmb(op(l), op(r))
case Leaf(_,x) => x
}
def weight(tree: CodeTree): Int = walk[Int](tree, weight, _ ++ _)
def chars(tree: CodeTree): List[Char] = tree match {
case Fork(l,r,x,_) => chars(l) ++ chars(r)
case Leaf(x,_) => List(x)
}
'weight'後面的括號看起來不對(特別是右括號後面的逗號)。你可能是指'走路(樹,體重,_ ++ _)'? – sepp2k
也只是看着它:我看到你編輯了代碼,它沒有解決這個問題? – Ossip
'weight'返回'Int',但你說'_ ++ _'。在'Int's上沒有'++'操作。我對嗎? –