访问树中的节点/叶子
我正在做一个我们刚刚开始学习的标准机器学习的作业问题,并且我一直在尝试计算树中叶子的总和。我如何访问这棵树的节点中的元素?
Im doing a homework problem in Standard ML which we just started learning, and im stuck trying to computer the sum of the leafs in a tree. How could i access the elements in the node of this tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过模式匹配来解构复杂的数据类型。大多数时候,您可以在函数参数中执行此操作。如果你的函数
f
接受一个列表作为参数,而不是像你现在所说的
那样定义它,当你向它传递一个列表时,它会自动将
x
分配给第一个元素,xs
到余数。在你的例子中,它看起来像这样:
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 asyou'd say
Now, when you pass it a list, it will automatically assign
x
to the first element, andxs
to the remainder.In your case, it's going to look something like this: