如何在ocaml中定义序列类型 [英] How the sequence type is defined in ocaml
本文介绍了如何在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的类型声明中,我们只能进行枚举或调用构造函数.
谢谢
What does 'a t = unit -> 'a node
mean in a type declaration? I thought that in a type declaration in ocaml we can only do an enumeration or call a constructor.
thank you
推荐答案
unit -> 'a node
是不带参数并返回'a node
(由'a
参数化的节点)的函数的类型.例子:
unit -> 'a node
is the type of a function that takes no argument and returns a 'a node
(a node parameterized by 'a
). Example :
let f () = Nil;;
type 'a t = unit -> 'a node
是上述类型的同义词,用于您的代码中定义的第一种类型.
type 'a t = unit -> 'a node
makes a synonym of the above type which is used in the first type defined in your code.
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>) *)
这篇关于如何在ocaml中定义序列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文