Find all elements in BST that satisfy f using continued success in SML

I have a task and I can’t understand how to make one question. Here is what I need to do:

Write a function that collects all the elements in the tree T, which satisfies the property p and returns it. Turn the tree in order. Find all elements in BST that satisfy f using continued success.

I have done 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 do not understand why it does not work ...

+3
source share
1 answer

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 great

+2
source

Source: https://habr.com/ru/post/1769393/


All Articles