如何在ocaml中定义序列类型

时间:2018-04-03 17:42:15

标签: ocaml

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中的类型声明中,我们只能进行枚举或调用构造函数。 谢谢

1 个答案:

答案 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>) *)