Recursion F#如何展平二叉搜索树

Recursion F#如何展平二叉搜索树,recursion,functional-programming,f#,tail-recursion,continuation-passing,Recursion,Functional Programming,F#,Tail Recursion,Continuation Passing,我有一棵树,结构如下: type 'a Tree =| Leaf of 'a| Branch of 'a Tree * 'a Tree 我在我的树上使用延续传递样式的尾部递归,并试图将其展平 let rec loop tree k acc = match tree with | Leaf v -> v :: acc | Branch (tl,tr) -> loop tl (loop tr k) acc loop xs id [] (Branch (Branch (

我有一棵树,结构如下:

type 'a Tree =| Leaf of 'a| Branch of 'a Tree * 'a Tree
我在我的树上使用延续传递样式的尾部递归,并试图将其展平

let rec loop tree k acc = 
  match tree with
  | Leaf v -> v :: acc
  | Branch (tl,tr) -> loop tl (loop tr k) acc
loop xs id []


(Branch (Branch (Leaf 1.0,Leaf 2.0),Branch (Leaf 3.0,Leaf 4.0)))
这只返回[1.0]


然而,我只得到树中的第一片叶子,我的函数在整个树上不起作用。我怎样才能做到这一点呢?

您传递的是一个延续,但您没有在任何地方调用它。试试这个:

let rec loop tree k acc = 
  match tree with
  | Leaf v -> k (v :: acc)
  | Branch (tl,tr) -> loop tl (loop tr k) acc
然后
循环xsid[]
生成
[4.0;3.0;2.0;1.0]