使用 SML 中的成功延续查找 BST 中满足 f 的所有元素

发布于 2024-09-27 07:22:28 字数 533 浏览 0 评论 0原文

我有一项作业要做,但我不知道如何做一道题。 这是我必须做的:

编写一个函数,收集树 T 中满足属性 p 的所有元素并返回它。按顺序遍历树。 使用成功延续查找 BST 中满足 f 的所有元素。

我做了以下事情:

datatype 'a tree = 
Empty | Node of (int * 'a) * 'a tree * 'a tree

fun find_all f Empty  cont = cont()
| find_all f (Node(x, l, r))  cont = if (f x) then find_all (f l (fn x => x::cont())) @ find_all (f r (fn x => x::cont()))
         else find_all (f l (fn () => cont())) @ find_all (f r (fn () => cont()));

我不明白为什么它不起作用......

I have an assignment to do and I can't figure out how to do one question.
Here is what I have to do:

Write a function which collects all elements in the tree T which satisfies the property p and returns it. Traverse the tree in inorder.
Find all elements in BST satisfying f using success continuations.

I did the following:

datatype 'a tree = 
Empty | Node of (int * 'a) * 'a tree * 'a tree

fun find_all f Empty  cont = cont()
| find_all f (Node(x, l, r))  cont = if (f x) then find_all (f l (fn x => x::cont())) @ find_all (f r (fn x => x::cont()))
         else find_all (f l (fn () => cont())) @ find_all (f r (fn () => cont()));

I don't understand why it's not working...

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

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

发布评论

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

评论(1

折戟 2024-10-04 07:22:28

这就是我所做的:

fun find_all f Empty cont = cont []
| find_all f (Node(x, l, r)) cont = if (f x) then find_all f l (fn e => find_all f r (fn t => cont (e@(x::t))))
                      else find_all f l (fn t => find_all f r (fn e => cont (e@t)));

而且效果很好

Here is what I did:

fun find_all f Empty cont = cont []
| find_all f (Node(x, l, r)) cont = if (f x) then find_all f l (fn e => find_all f r (fn t => cont (e@(x::t))))
                      else find_all f l (fn t => find_all f r (fn e => cont (e@t)));

And it works fine

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