I have to make a recursive function that counts the number of nodes of a binary tree of three diferent types, and I am supose to save them in a result with the following type int * int * int, I think my reasoning is right.
type dtree =
Decision of string * int * dtree * string * int * dtree
| Chance of string * int * dtree * string * int * dtree
| Outcome of int
;;
let rec count dt =
match dt with
Decision(choiceL, costL, l, choiceR, costR, r) -> (x+1,y,z) count l count r
| Chance(eventL, probL, l, eventR, probR, r) -> (x,y+1,z) count l count r
| Outcome value -> (x,y,z+1)
;;
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.