0
votes

Here is my code

type arbre= Feuille of int | Noeud of arbre*int*arbre;;
let monarbre= Noeud(Noeud(Feuille(1),2,Noeud(Feuille(6),9,Feuille(7))),4,Feuille(8));;

let rec occ n a= let cpt=0 in
    match a with
    Feuille _ -> 0
    |Noeud(g,v,d)-> if v=n then cpt+1
    else if occ n g then cpt+1
    else if occ n d then cpt+1
;;

i want to counts the occurence in this tree but always have a error message like this

File "main.ml", line 8, characters 12-19: Error: This expression has type int but an expression was expected of type bool Can someone help me?

1

1 Answers

0
votes

Your function is computing an integer value, the number of occurrences of n in a. So this expression:

if occ n g then ...

can't be correct. The expression between if and then must have a boolean type. Your expression has int type. This is what the compiler is telling you.

In fact it seems to me you're interested in the specific value returned by occ n g. Let's say it returns 6, I suspect you want to return a value that's bigger by 6, not bigger by 1.

You have at least one other error: there needs to be an else part for your final if. You need to specify what value your function should have in this case.