0

我有一个数据类型:

datatype 'a tree = LEAF of 'a 
            | NODE of 'a tree * 'a tree;

我希望创建一个名为 maptree(f) 的函数,它返回一个匿名函数,该函数能够在树上按元素执行 f。为什么以下不起作用?

fun maptree(f) = fn LEAF(a) => LEAF(f(a))
                  | NODE((b,c)) => NODE(f(b), f(c));

我得到错误:

stdIn:56.7-56.65 Error: types of rules don't agree [circularity]
earlier rule(s): 'Z tree tree -> 'Y tree tree
this rule: 'Z tree -> 'Y tree
in rule:
NODE (b,c) => NODE (f b,f c)
4

1 回答 1

3
f : 'a -> 'b

所以你不能把它应用到树上。你可能想要。

fun maptree f = fn LEAF a => LEAF (f a)
                 | NODE(b,c) => NODE (maptree f b, maptree f c);
于 2013-10-21T00:19:38.183 回答