【发布时间】:2013-01-28 19:46:33
【问题描述】:
我正在尝试使用尾递归本地辅助函数作为分配的一部分重写代码。
all_except_option 是一个返回类型为 fn 的函数:string * string list -> string list option
fun all_except_option ([], _ ) = NONE
| all_except_option (_, "") = NONE
| all_except_option (str_list, str) =
let
fun all_except_option' [] = []
| all_except_option' (x::str_list) =
if x = str then
all_except_option' str_list
else
x :: all_except_option' str_list
in
SOME (all_except_option' str_list)
end;
下面的函数是没有使用尾递归局部辅助函数的函数
fun sub1 ([], s) = []
| sub1 (x :: xs, s) =
case all_except_option(x, s) of NONE => sub1(xs, s) //
| SOME y => y @ get_substitutions1(xs, s);
此函数使用尾递归,但是在递归调用辅助函数时出现错误。错误是: 错误:非构造函数应用于模式中的参数:all_except_option
fun get_substitutions2 (s,str) =
let fun aux(s,x::xs,acc) =
case x of [] => acc
| all_except_option(x, s) => aux(s,xs,xs::acc)
in
aux(s,str,[])
end;
【问题讨论】:
-
您遇到什么错误?您能否发布显示所有功能的完整代码?此外,乍一看,
xs::acc会失败,因为xs似乎是一个列表。 -
添加功能和错误
-
coursera 课程有一个论坛,其中有一个专门讨论课程作业的部分。这是讨论家庭作业的更好地方
标签: sml tail-recursion