错误 - 通过传递列表递归函数调用 - OCaml的
问题描述:
我想创建包含在OCaml的列表字符串的排列。 到目前为止,我一直在处理以下代码片段,但面临着将列表的第一个字符串传递给我的方法的问题。错误 - 通过传递列表递归函数调用 - OCaml的
逻辑的代码: 迭代到列表中的每个元素,并且每个元素与所述列表的元素追加。继续执行,直到所有元素都被添加到列表中的每个可能位置。
代码:
(* this function appends each string to each word in the list example: "A" with "ABC" *)
let appendtocode n word =
let f x = n^x in
f word
;;
(* this function extracts every element of the list and appends it with the string.
Example: "A" with ["AAA","ABC","ACD"] etc.. *)
let appendtolist n list =
let f x =
if (List.length list) > 0 then list
else ((appendtocode n (List.hd list))^(appendtolist n (List.tl list)))
in
List.map f list
;;
错误:
我得到这个错误:
没有限制值appendtolist
发生在调用:(appendtolistñList.tl名单)
我的列表只是由字符串组成。 我仍在处理代码。但由于这个错误而停留在此处。
请帮忙!!!任何输入都会很棒。
答
要递归调用的函数,你需要用let rec appendtolist
,而不是仅仅let appendtolist
定义它。
然后你会得到一个不同的错误,因为在你的代码其他错误...
答
我不知道SML很好,但我认为你需要更多的括号,例如
else ((append-to-code n List.hd list)^(append-to-list n List.tl list))
应该
else ((append-to-code n (List.hd list))^(append-to-list n (List.tl list)))
答
因为你递归调用appendtolist
不宣递归你得到了“未绑定值appendtolist”的错误。
你需要写let rec appendtolist n list = ...
能够参考appendtolist
递归中它的定义。
这是为什么标签SML和smlnj如果你使用ocaml的? – sepp2k 2010-11-27 23:40:04
我修复了标签,因为这绝对是OCaml,与SML没有明显的联系。 – Porculus 2010-11-27 23:42:14