我很乐意听取一个例子的有用评论:
http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc79
7.12 显式多态类型注释
type 'a t = Leaf of 'a | Node of ('a * 'a) t
let rec depth : 'a. 'a t -> 'b = function
|Leaf _ -> 1
| Node x -> 1 + depth x
我理解这个例子函数,但当我尝试定义一个类型为“map-like”的函数时,遇到了困难。
'a. 'a t -> ('a -> 'b) -> 'b t
e.g.:
let rec tmap: 'a. 'a t ->(f:'a->'b) -> 'b t = function
|Leaf x -> Leaf( f x)
|Node x -> let res = tmap x in Node(res);;
I get the following error:
Characters 67-77:
|Leaf x -> Leaf( f x)
^^^^^^^^^^
Error: This expression has type 'c t but an expression was expected of type
(f:'a -> 'b) -> 'b t
我并不完全理解这个问题。如果有任何有用的评论,我将不胜感激。