我有下一个代码:
datatype expr = K of string| Number2 of expr * (expr list);
datatype number = Number1 of string | Number3 of int;
fun append (nil, l2) = l2
| append (x::xs, l2) = x::append(xs, l2);
fun map [] = []
| map (h::t) = (What h)::(map t);
fun What (K x) = [Number1(x)]
|What (Number2 (t,[])) = Number3(0)::What(t)
|What (Number2 (y,a::b)) = append(What(a), map(b));
它无法识别函数“What”。(未绑定(bind)的变量或构造函数)。我该如何修复它,让它知道“什么”功能?
谢谢。
最佳答案
SML 中的声明从上到下工作,因此 map
看不到 What
。切换顺序无济于事,因为 What
不会看到 map
,给出同样的错误。相反,您需要使用 and
同时声明相互递归的函数:
fun map [] = []
| map (h::t) = (What h)::(map t)
and What (K x) = [Number1(x)]
| What (Number2 (t,[])) = Number3(0)::What(t)
| What (Number2 (y,a::b)) = append(What(a), map(b))
关于SML - 未绑定(bind)的变量或构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8009873/