I have a datatype trie = Node of char * (trie ref) list | Empty And I want to collect all the words in the trie, using these two mutually recursive functions:
words_in_trie: trie -> (char list list -> 'a) -> 'a
all_words: trie ref list -> (char list list -> 'a) -> 'a
and then calling them with fun all_entries t = all_words t (fn l => map (fn w => String.implode w) l);
This has to be done with continuations. I've written it in non-continuation form, as follows:
fun wt Empty = [[]]
|wt (Node(c,rl)) = map (fn (l) => c::l) (aw rl)
and aw [] = []
|aw [h] = wt (!h)
|aw (h::t) = (wt (!h))@(aw t)
But I can't figure out how to convert them into continuation form! This is what I have so far, but it doesn't work:
fun words_in_trie Empty cont = cont[]
|words_in_trie (Node(c,rl)) cont = (all_words rl (fn r=> cont(c::r)))
and all_words [h] cont = words_in_trie (!h) cont
|all_words (h::t) cont = (words_in_trie (!h) cont)@(all_words t cont)
I've been stuck on this for ages, I will appreciate any help.