English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Frustrated Beginner
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-12-24 (09:49)
From: skaller <skaller@o...>
Subject: Re: [Caml-list] Frustrated Beginner
On Tue, 2003-12-23 at 15:03, Tyler Eaves wrote:

> So why is O'Caml giving me so much trouble? 

Because the syntax is foreign.

> My biggest source of problems seems to be the syntax. I'm totally
> confused as far as ; vs ;; vs nothing, 

Yeah. The 'top level' is syntactically and semantically different
to the inner levels of Ocaml.

(1) At the top level you can write a sequence of definitions
or evaluate a sequence of expressions. This is not possible
anywhere else.

(2) If the definition/expression starts with a keyword
you don't need to use ';;' before it. For example:

	let f x = x
	let g x = x * x
	;;          (* needed because .. *)
	f x * g x   (* no keyword starts this *)
	;;          (* needed to force evaluation of expression NOW *)

At the inner level, you cannot write

	let h x =
		let f x = x
		let g x = x * x
		f x * g x

Inner expressions are evaluated 'entirely', not one at a time like
the top level. So you must write this:

	let h x =
		let f x = x in
		let g x = x * x in
		f x * g x
	;; (* end top level definition *)

You use ';' for sequencing evaluations:

	print_endline "Hello";
	print_endline "World"

This is a *single* expression evaluated, whereas

	print_endline "Hello"
	print_endline "World"

is two evaluations. (Try this in the 'ocaml' interpreter).

> when to use ( ), and things of
> the like. It doesn't help that the compiler is completly naive when it
> comes to Syntax Errors. 

The reason is: the parser is a Yacc tool which means it
uses a LR parsing technique. When it can't parse something,
it has no choice than to say 'syntax error'. Its an error,
there's no way to say what the error is.

One way around this in LR parsers is to include error
productions in the grammar... that is, make the parser
parse common errors.

Another way, is to parse something more general than 
permitted first, then check constraints. This is certainly
done with type checking for example.

To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners