type 'a node =
| Nil
| Cons of 'a * 'a t
and 'a t = unit -> 'a node
type 'a mappable = 'a t
'a t = unit -> 'a node
在类型声明中的含义是什么?我认为在ocaml中的类型声明中,我们只能进行枚举或调用构造函数。
谢谢
答案 0 :(得分:2)
unit -> 'a node
是不带参数的函数类型,返回'a node
(由'a
参数化的节点)。示例:
let f () = Nil;;
type 'a t = unit -> 'a node
是上述类型的同义词,它在代码中定义的第一种类型中使用。
let l = Cons (4, fun () -> Cons (3, fun () -> Nil));;
let Cons(_,ll) = l;; (* ok , just for example, it returns a warning due to incomplete pattern matching *)
ll ();; (* - : int node = Cons (3, <fun>) *)