How the sequence type is defined in ocaml

霸气de小男生 提交于 2020-01-06 05:52:32

问题


type 'a node =
  | Nil
  | Cons of 'a * 'a t

and 'a t = unit -> 'a node

type 'a mappable = 'a t

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


回答1:


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


来源:https://stackoverflow.com/questions/49636168/how-the-sequence-type-is-defined-in-ocaml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!