我在标准 ML 中做一个家庭作业问题,我们刚刚开始学习,我坚持尝试计算一棵树中叶子的总和。我如何访问这棵树的节点中的元素?
1 回答
0
You deconstruct complex data types through pattern matching. Most of the time, you do this in your function arguments. If your function f
takes a list as an argument, instead of defining it as
fun f X = ...
you'd say
fun f x::xs = ...
Now, when you pass it a list, it will automatically assign x
to the first element, and xs
to the remainder.
In your case, it's going to look something like this:
fun sum Node(element,children)::siblings = ...
于 2012-01-30T04:25:06.607 回答