访问树中的节点/叶子

发布于 2024-12-29 19:49:35 字数 137 浏览 4 评论 0原文

我正在做一个我们刚刚开始学习的标准机器学习的作业问题,并且我一直在尝试计算树中叶子的总和。我如何访问这棵树的节点中的元素?

在此处输入图像描述

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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过气美图社 2025-01-05 19:49:35

您可以通过模式匹配来解构复杂的数据类型。大多数时候,您可以在函数参数中执行此操作。如果你的函数f接受一个列表作为参数,而不是像

fun f X = ...

你现在所说的

fun f x::xs = ...

那样定义它,当你向它传递一个列表时,它会自动将x分配给第一个元素,xs 到余数。

在你的例子中,它看起来像这样:

fun sum Node(element,children)::siblings = ...

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 = ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文