nested if -else loop error - ocaml

好久不见. 提交于 2019-12-11 07:16:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!