如果-else循环错误,则嵌套-ocaml [英] nested if -else loop error - ocaml
问题描述
我正在尝试为我的代码制定一个多次if-else循环.
I am trying to work out a multiple if-else loop for my code.
我以前的代码是:
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
;;
带有嵌套循环的修改后的代码:
Modified code with nested loops:
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循环的示例,该示例运行良好.
My code seems to be syntactically correct. Is this the right way to implement a nested loop in OCaml?? I followed an example for if-elseif loop found online which worked fine.
我最终需要输出x
,该列表是此函数中对appendtocode
和appendtolist
的所有递归调用之后形成的列表.
I need to finally output x
which is the list formed after all the recursive calls to appendtocode
and appendtolist
in this function.
我在哪里出错了?
请指导.
谢谢.
推荐答案
在您的第一个代码示例中,您像这样调用appendtocode
:
In your first code sample you're calling appendtocode
like this:
appendtocode n (List.hd list)
所以我假设appendtocode
是一个带有2个参数的函数.
So I assume that appendtocode
is a function taking 2 arguments.
在第二个步骤中,您将这样称呼它:
In the second you're calling it like this:
appendtocode n (List.hd list) (b-1)
因此,您在这里使用3个参数来调用它.由于只需要两个,因此您会收到一条错误消息,告诉您您使用了太多的参数来调用它.
So here you're calling it with 3 arguments. Since it only takes two, you get an error message telling you that you're calling it with too many arguments.
PS:如果语句不是循环
PS: If statements aren't loops.
这篇关于如果-else循环错误,则嵌套-ocaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!