1) the " | " pattern matching syntax
On the book, it's
let rec eval env exp =
match exp with
Const c -> c
| Var v ->
(try List.assoc v env with Not_found -> raise (Unbound_variable v))
| Sum (f, g) -> eval env f +. eval env g
| Diff (f, g) -> eval env f -. eval env g
| Prod (f, g) -> eval env f *. eval env g
| Quot (f, g) -> eval env f /. eval env g;;
but I could also write:
let rec eval env exp =
match exp with
| Const c -> c
| Var v ->
(try List.assoc v env with Not_found -> raise (Unbound_variable v))
| Sum (f, g) -> eval env f +. eval env g
| Diff (f, g) -> eval env f -. eval env g
| Prod (f, g) -> eval env f *. eval env g
| Quot (f, g) -> eval env f /. eval env g;;
with a " | " before "Const" , which also works.
Are they of the same semantics?
2) when to use ";"?
I guess ";" means the break of expressions. So basically it's used in
compound data types or imperative programs with side effect.
Am I right?
Regards,
Yes; it's just a potential add-in for when it makes the synax prettier.
Consider the following...
let rec factorial = function 1 -> 1 | x -> x*factorial (x-1)
let rec factorial = function
| 1 -> 1
| x -> x * factorial (x-1)
So, yes, it's just a prettifying syntax.
> I guess ";" means the break of expressions. So basically it's used in
> compound data types or imperative programs with side effect.
Exactly.