我有一个数据类型:
datatype 'a tree = LEAF of 'a
| NODE of 'a tree * 'a tree;
我希望创建一个名为maptree(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)
答案 0 :(得分:3)
f : 'a -> 'b
因此您无法将其应用于树。你可能想要。
fun maptree f = fn LEAF a => LEAF (f a)
| NODE(b,c) => NODE (maptree f b, maptree f c);