我是Ocaml的新手,希望获得有关递归数据类型定义的递归函数的帮助。我已经定义了如下数据类型
type 'a slist = S of 'a sexp list
and
'a sexp = Symbol of 'a | L of 'a slist
我正在编写的函数(subst)检查定义的slist中的符号a并用b替换它。对于ex subst 1 10 S [1; 4; S [L [3; 1;]; 3];]返回S [10; 4; S [L [S [3; 10;]]; 3] 。我的代码如下
let rec subst a b sl = match sl with
S s -> match s with
[] -> []
| p :: q -> match p with
Symbol sym -> if sym = a then S[b] :: (**subst** a b S[q]) else
S[p] :: (subst a b S[q])
L l -> (subst a b l) :: (subst a b S[q])
| L lis -> subst a b lis;;
我收到错误:
此函数适用于太多参数;也许你忘记了';'
请帮忙
答案 0 :(得分:1)
您的类型可以更简单的方式定义,您不需要slist
:
type 'a sexp = Symbol of 'a | L of 'a sexp list
您的问题是subst a b S[q]
被读为subst a b S [q]
,即应用于4个参数的函数subst。您必须改为编写subst a b (S[q])
。