2

首先是一些背景:我正在玩这个优秀KFoldTree博客文章。我有一个 n 元而不是二进制的树结构。使用二叉树,将 CPS 变换应用于传递给的函数KFoldTree并不难。你最终会得到类似的东西:

kleft (fun left -> kright (fun right -> k (dosomethingwith left right)))

n 叉树的问题是你必须即时构建这个延续链。你想要结束的应该是这样的:

kchildren (fun children -> k (dosomethingwith children))

其中children是折叠结果类型的列表。例如,如果我正在编写一个漂亮的打印机,children应该是 type string list,而kchildren应该是 type (string list -> string) -> string

那么我们如何定义一个产生 的函数kchildren,给定一个((Node -> string) -> string) list(继续漂亮的打印机示例)?

4

1 回答 1

0

这就是我想出的:

let chain continuations cont =
    let rec loop acc =
        function
        | k::ks -> k (fun x -> loop (x::acc) ks)
        | [] -> cont acc
    // Reverse the input list to preserve original left-to-right order.
    List.rev continuations |> loop []

有没有更好的办法?

于 2016-09-21T22:00:34.117 回答