【发布时间】:2012-03-15 18:35:55
【问题描述】:
我有一个 数据类型 trie = char 的节点 * (trie ref) 列表 |空的 我想收集trie中的所有单词,使用这两个相互递归的函数:
words_in_trie: trie -> (char list list -> 'a) -> 'a
all_words: trie ref list -> (char list list -> 'a) -> 'a
然后用 fun all_entries t = all_words t (fn l => map (fn w => String.implode w) l);
这必须通过延续来完成。我写成非续写形式,如下:
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)
但我不知道如何将它们转换为延续形式! 这是我到目前为止所拥有的,但它不起作用:
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)
我多年来一直坚持这一点,我将不胜感激。
【问题讨论】:
-
标准 ML per se 没有延续。你是在使用Standard ML of New Jersey's
SMLofNJ.Contstructure,还是其他地方的类似模块,还是自制的? -
使用作为函数的延续 (char list list -> 'a),它将列表列表作为输入,表示到目前为止收集的单词的后缀
标签: recursion sml trie smlnj continuation