views:

52

answers:

2

Hi,

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...

Thank you very much for your help!

A: 

Actually I found the answer! Thanks

Matt1510
Telling us the answer would allow us to upvote you.
Frank Shearar
+1  A: 

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

Matt1510