我有一项作业要做,但我不知道如何做一道题。 这是我必须做的:
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/