问题
I am trying to work out a multiple if-else loop for my code.
My previous code was:
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
;;
But I get this error:
This function is applied to too many arguments, maybe you forgot a `;'
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.
I need to finally output x which is the list formed after all the recursive calls to appendtocode and appendtolist in this function.
Am I going wrong anywhere??
Please guide.
Thank you.
回答1:
In your first code sample you're calling appendtocode like this:
appendtocode n (List.hd list)
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)
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: If statements aren't loops.
来源:https://stackoverflow.com/questions/4299102/nested-if-else-loop-error-ocaml