I see many problems in your code, but it might be better if you asked a specific question. Just as a place to start, you're using the names x
, y
, and z
without defining them anywhere.
The key to your problem, I think, is that you need to give names to the values returned by the recursive calls count l
and count r
. Once they have names you can use them in the result at the current level of the tree.
Update
Here's a function that adds up the values in a list of pairs. It has the same rough structure as what you're looking for:
let rec sumpairs pairs =
match pairs with
| [] -> (0, 0)
| (x, y) :: tail ->
let (subx, suby) = sumpairs tail in
(subx + x, suby + y)