Find all elements in BST satisfying f using success continuations in SML
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()))
el开发者_JS百科se find_all (f l (fn () => cont())) @ find_all (f r (fn () => cont()));
I don't understand why it's not working...
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
精彩评论