【发布时间】:2016-09-26 16:52:40
【问题描述】:
我有一个函数叫map_tree,如下:
def fold_tree[A,B](f1: A => B) (f2: (A,B,B) => B) (t: Tree[A]) : B = t match {
case Leaf(value) => f1(value)
case Node(value , l, r) => f2 (value, fold_tree (f1) (f2) (l), fold_tree (f1) (f2) (r) )
}
我需要实现一个名为right_most 的函数,它接受Tree[A] 并返回A。这是我的尝试:
def right_most [A](t:Tree[A]) : A =
fold_tree ((x: A) => x) ((v: (A, A, A)) => v._3) (t)
但我收到以下错误:
found : ((A, A, A)) => A
required: (A, A, A) => A
fold_tree ((x: A) => x) ((v: (A, A, A)) => v._3) (t)
^
one error found
在我看来,found 和 required 是一样的。那有什么错误呢?另外,我们如何在匿名函数中指定元组类型?以及为什么我需要在函数签名中指定元组类型。 scala不能推断吗?
【问题讨论】:
-
fold_tree定义是什么? -
抱歉,不小心包含了 map_tree。将其替换为 fold_tree。
标签: scala tree functional-programming tuples