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

标签 tree sml continuations

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

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.

我做了以下事情:

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()));

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

最佳答案

这是我做的:

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)));

而且效果很好

关于tree - 使用 SML 中的成功延续查找 BST 中满足 f 的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3926526/

相关文章:

algorithm - 戈朗 : benchmark Radix Tree Lookup

c++ - 与 ctors 类 union 的替代方案

python - 从闭包表 SELECT 语句渲染树?

SML 如何检查变量类型?

SML [圆度] 错误

c# - 异步/等待 - 是*并发*吗?

java - Java 中的延续

Haskell 中序遍历树 前序 后序

types - SML 算术函数的类型被推断为 int

rust - Rust 中的可恢复连续传递样式迭代器减少