开发者

nested if - else statements -ocaml

can we do multiple operations inside our if-else loop in ocaml?

I am trying to do the following:

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)) (* THIS LINE *)

        else ((appendtocode n (List.hd list)) :: (appendtolist n (List.tl list) (b-1)) )

    in

    f list

    ;;

I get a complain by the compiler on the line which I made bold that :

This expression is not a function, it cannot be applied

I want to call my function when b==0 and also decrement value of b.

How could I do it in this case?

Plea开发者_开发技巧se advise.

Thank you.


This is basically the same error as before.

You're evaluating

appendtocode n (List.hd list)

which returns a value, which is not a function. Then you try to call it with b-1 as its argument, but since it's not a function, you can't do that.

I want to call my function when b==0 and also decrement value of b.

Decrement b for whom? b is an argument to the function appendtolist, so if you call appendtolist recursively, you can supply a new argument for b, which is what you're doing in the else case and that works fine. But in the then case you're not calling appendtolist. You're only calling appendtocode and appendtocode has not third argument b. So passing it b-1 simply makes no sense.

My guess is that your program will work just fine if you remove the b-1.

PS: This issue is entirely unrelated to nested ifs.


Quick guess:

The expression given as

( appendtocode n (List.hd list) )

returns something which is not a function, and hence you can't apply the value of (b-1) to it which is what you are trying to do. To verify, you can look up the type of appendtocode and see it it can take two or three curried arguments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜