Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

a little about syntax.

0 views
Skip to first unread message

Pebblestone

unread,
Nov 12, 2006, 8:49:24 PM11/12/06
to
I'm new to OCaml, and I just finished reading the core language.
I have 2 question on its syntax.

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,

Tiogshi Laj

unread,
Nov 13, 2006, 4:04:30 PM11/13/06
to

> Are they [inclusion or omission of | before first term of a match or function case clause] of the same semantics?

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.

Pebblestone

unread,
Nov 14, 2006, 12:10:18 AM11/14/06
to
Thanks for the explanation. :)
0 new messages