Ocaml-错误的类型 [英] Ocaml - wrong type
本文介绍了Ocaml-错误的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查树是否平衡(这意味着每片叶子都在同一深度),但类型有问题.
I want check if tree is balanced (It means that each leaf is on the same depth) but I have problem with wrong type.
type 'a tree = Node of 'a * 'a tree list;;
let rec fold_tree f (Node (x,l)) =
f x (map (fold_tree f) l);;
let is_balanced t =
fst(
fold_tree
(fun _ (l: (bool * int) list ) ->
((fold_left
(fun h (flag,first_value)->
((fst h)=first_value)&&flag,first_value)
(true,snd(hd l) )
l))
)
t);;
问题在那里:
((fold_left(fun h (flag,first_value)-> ((fst h)=first_value)&&flag,first_value) (true,snd(hd l) ) l))
Ocaml告诉我这是bool * bool的类型,但是我确信这是bool * int的类型,因为l是(bool * int)列表的类型,所以hd l是(bool * int)的类型,所以snd (hd l)是int类型...
Ocaml tells me that this is type of bool * bool but I am convinced that this is type of bool * int because l is type of (bool * int) list so hd l is type of (bool * int) so snd(hd l) is type of int...
推荐答案
一些建议:
- 命名您的中介职能
- 避免打开
List
- 避免使用
List.hd
(以便正确处理空列表情况) - 相信类型检查器
- 在使用typechecker帮助进行调试时使用类型注释
- Name your intermediary functions
- Avoid opening
List
- Avoid using
List.hd
(in order properly handle the empty list case) - Believe the typechecker
- Use type annotations when debugging with the typechecker help
以您为例,您应该看看内部函数的类型
In your case, you should have a look at the type of your inner function
fun (h:'a) (flag,first_value): 'a->
(fst h=first_value) && flag,first_value
这篇关于Ocaml-错误的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文