Tree 使用SML中的success continuations查找BST中满足f的所有元素
我有一个作业要做,我不知道怎么做一个问题。 以下是我必须做的: 编写一个函数,该函数收集树T中满足属性p的所有元素并返回它。按顺序遍历树。 使用success continuations查找BST中满足f的所有元素 我做了以下工作:Tree 使用SML中的success continuations查找BST中满足f的所有元素,tree,sml,continuations,Tree,Sml,Continuations,我有一个作业要做,我不知道怎么做一个问题。 以下是我必须做的: 编写一个函数,该函数收集树T中满足属性p的所有元素并返回它。按顺序遍历树。 使用success continuations查找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))
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)));
而且效果很好