小编equ*_*rts的帖子

OCaml中的fold_tree

您可能知道,OCaml中有更高阶的函数,例如fold_left,fold_right,filter等.

在我的函数式编程课程中引入了一个名为fold_tree的函数,它类似于fold_left/right,不在列表上,而是在(二元)树上.它看起来像这样:

 let rec fold_tree f a t = 
  match t with
    Leaf -> a |
    Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);;
Run Code Online (Sandbox Code Playgroud)

树被定义为:

type 'a tree = 
  Node of 'a tree * 'a * 'a tree | 
  Leaf;;
Run Code Online (Sandbox Code Playgroud)

好的,这是我的问题:fold_tree函数如何工作?你能给我一些例子并用人类语言解释吗?

ocaml functional-programming higher-order-functions

8
推荐指数
2
解决办法
6483
查看次数