相关疑难解决方法(0)

尾递归函数,用于在Ocaml中查找树的深度

我的tree定义类型如下

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

我有一个函数来查找树的深度如下

let rec depth = function 
    | Leaf x -> 0
    | Node(_,left,right) -> 1 + (max (depth left) (depth right))
;;
Run Code Online (Sandbox Code Playgroud)

这个函数不是尾递归的.有没有办法让我以尾递归的方式编写这个函数?

tree binary-tree ocaml functional-programming

33
推荐指数
2
解决办法
6694
查看次数

标签 统计

binary-tree ×1

functional-programming ×1

ocaml ×1

tree ×1