Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] baffled by semicolon
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-08-30 (08:07)
From: skaller <skaller@u...>
Subject: Re: Baffeld by manual (Was: [Caml-list] baffled by semicolon)
On Mon, 2004-08-30 at 17:12, Florian Hars wrote:
> wrote:
>  > I went back through the manual and really couldn't find anything which
>  > explained the difference between ; and ;;

The single ; is (usually) left associative binary sequencing
operator of type unit that takes two expressions of 
type unit as an argument:

	e1 ; e2

evaluates e1, then e2 (for side effects).
I said 'usually' because it has another role
in some contexts -- [();()] is a list of two
units whereas [(();())] is a list of one unit.
[This is like the ugly C hackery with , ]

The top level of Ocaml 'executes' statements in sequence.
For example:

	let _ = e1
	let _ = e2

The role of ;; has nothing to so with top level sequencing:
the sequencing is already built in.

The purpose of ;; is much simpler -- it is nothing
more than a piece of punctuation marking the end
of a statement.

As you can see above it is not required if 

(a) there is a next statement and
(b) that next statement starts with a keyword

You may need ;; in the interpreter to tell it
'there isn't another statement, start evaluating'.

You may also need it if you use a statement
not starting with a keyword, for example here:

	let x = ref 0 
	;; x := 1 (* doesn't start with a keyword *)

where you can see that ;; is more or less the keyword
you use to start a statement when it doesn't start
with a keyword.. that isn't quite correct, since you
don't need one at the start (its really a separator
which is 'infered' when the parser hits an unexpected
keyword :)

This code is actually very ugly because you can't
lift it out of the top level.

	let _ =
		let x = ref 0 in
		x := 1

is the way to do this properly -- and here the x := 1
is an expression contained in the let/in expression.

John Skaller,
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: