我想创建一个遍历二叉树的函数,验证某些条件,然后返回一个bool。
val valid : dtree -> bool
type dtree =
Decision of string * int * dtree * string * int * dtree
| Chance of string * int * dtree * string * int * dtree
| Outcome of int
;;
let rec valid dt =
match dt with
Decision(choiceL, costL, l, choiceR, costR, r) -> if choiceL=choiceR then false else valid l valid r
| Chance(eventL, probL, l, eventR, probR, r) -> if eventL=eventR && (probL + probR)<>100 then false else valid l valid r
| Outcome value -> true
;;
这给了我以下错误:
Decision(choiceL, costL, l, choiceR, costR, r) -> if choiceL=choiceR then false else valid l **valid** r Error: This expression has type dtree -> 'a -> 'b -> 'c but an expression was expected of type 'a # Interrupted.
答案 0 :(得分:1)
如果我输入您的代码,我就不会收到您说的错误。
通过眼睛看,这段代码看起来不对:
valid l valid r
我希望更像是:
valid l && valid r