Reputation: 63
When I have e.g.
type 'a inf_list = Cons of 'a * (unit -> inf_list);;
(* inf_list starting at n*)
let rec num n = Cons (n, fun () -> num (n+1));;
How does the following work:
let Cons (cur, rest_l) = integers 1;;
let x = cur;;
let y = (rest_l());;
normally the format is let <variable/function-name> = <definition>
.
Here it is let <type (x,y) = <expression>
. So generally speaking how does this work?
Upvotes: 1
Views: 146
Reputation: 36516
As an addendum to Jeffrey's answer, please note that the as
keyword can be used to bind a name to a pattern.
In your case:
let Cons (cur, rest_l) as lst = integers 1;;
Where lst
now refers to the entire inf_list
value as well as binding names to its constituent values.
Upvotes: 1
Reputation: 66793
First of all, the form for let
is let <pattern> = <expression>
. You can have many things in a let
besides variable and function names. A name is just one possible kind of pattern.
Second, the subexpression Cons (cur, rest_l)
is not a type. It's a pattern that matches a value of type 'a inf_list
. The head of the list will be bound to cur
and the rest of the list will be bound to rest_l
.
So your example has the expected form for a let
. And it binds the two names cur
and rest_l
in the global scope.
Upvotes: 2