我必须只使用函数范式计算OCaml中n元树的深度,而不使用外部自制函数。结构如下:
type nTree =
Id of int
| Leaf of string
| Tree of string * string * nTree list
下面是我的结果:
let rec height t = match t with
| Id _ -> 0
| Leaf _ -> 0
| Tree(_,_,n) -> if n = [] then 1
else let e::r = n in max
(List.fold_left (fun acc x -> acc + height x) 1 [e])
(List.fold_left (fun acc x -> acc + height x) 1 r)
这很管用,但我觉得很难看
e::r
位由于与
[]
图案
有没有办法让这个警告变得免费和“漂亮”?
谢谢!