问题
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