我正在尝试在 OCaml 中创建一个函数,在给定一个 n-tree 的情况下,它返回一个列表,其中包含从叶子到所有分支的根的所有总和。这就是我所做的:
exception NotFound
type 'a ntree = Tr of 'a * 'a ntree list
let leaf x = Tr (x, [])
let alb =
Tr (1, [Tr (2, [leaf 3; leaf 4; leaf 2]);
Tr (5, [leaf 11; leaf 10]);
Tr (3, [leaf 9; leaf 7; leaf 10])])
let rec peso (Tr (x, tlist)) =
match tlist with
[] -> [x]
| _ -> [x + peso_l tlist]
and peso_l = function
[] -> raise NotFound
| [t] -> peso t
| t::rest -> peso t :: peso_l rest
但它不起作用,因为,我认为,
| _ ->[x + peso_l tlist]
返回类似的东西[x+ [t]]
(我是对的吗?)。我该如何解决?