嵌套如果-else循环错误 - ocaml
问题描述:
我想解决我的代码多个if-else循环。嵌套如果-else循环错误 - ocaml
我以前的代码为:
let rec appendtolist n list b =
let f x =
if (b == 0) then x
else (append (appendtocode n (List.hd list)) (appendtolist n (List.tl list) (b-1)))
in
f list
;;
修改后的代码与嵌套循环:
let rec appendtolist n list b =
let f x =
if b < 0 then x
else if (b == 0) then appendtocode n (List.hd list) (b-1)
else appendtocode n (List.hd list) :: appendtolist n (List.tl list) (b-1)
in
f list
;;
但我得到这个错误:
This function is applied to too many arguments, maybe you forgot a `;'
我的代码似乎是语法正确。这是在OCaml中实现嵌套循环的正确方法吗? 我接着找到了一个在线查找if-elseif循环的例子,它工作正常。
我需要最终输出x
这是在该函数中所有递归调用appendtocode
和appendtolist
后形成的列表。
我在哪里错了?
请指导。
谢谢。
答
在你的第一个代码示例你打电话appendtocode
这样的:
appendtocode n (List.hd list)
所以我认为appendtocode
是服用2个参数的函数。所以在这里
appendtocode n (List.hd list) (b-1)
你有3个参数调用它:
在第二个你调用它像这样。由于它只需要两个参数,因此会收到一条错误消息,告诉您您调用的参数太多。
PS:如果语句不是循环。
嗨。谢谢。没有意识到语法错误。我将这个问题转贴到新帖子中。而不是在这里覆盖它。再次感谢。 – JJunior 2010-11-28 21:26:30