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

RE: Undefined evaluation order
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2000-10-11 (20:45) From: Pierre Weis Subject: Re: Undefined evaluation order
```[...]
> As I mentioned earlier, when teaching, it's nice for
> a language to be simple and uniform.  Explaining to
> a student why:
>
> 	let x = input() in
> 	let y = input() in
> 	(x,y)
>
> is not equivalent to:
>
> 	(input(), input())
>
> is one more thing that confuses them -- especially when
> we emphasize that the whole point of anonymous functions
> is to avoid naming things that need not be named!
[...]
> -Greg

I totally agree with all your comments, and would like to report the
most difficult example I know to confuse students (and sometimes
teachers!).

Consider to explain the naive definition of map:

let rec map f = function
| [] -> []
| x :: l -> f x :: map f l;;

Now, you map a trivial function as an example:

let add i = i + 1;;

- : int list = [2; 3; 4]

Seems perfectly good and ok. Your students are happy.

Then, suppose you explain the use of printf to debug functions, by
a simple modification of their code:

let add i = Printf.printf "argument %d\n" i; i + 1;;

argument 1
- : int = 2

Now, unfortunately one of them is a brave soul and types in something

argument 3
argument 2
argument 1
- : int list = [2; 3; 4]

After 10 seconds, the brave soul is claiming ``urbi et orbi'': I've
found a bug in Caml!

Now, try to explain to your students (that are now at leat 12 standing
up and gazing at the brave soul's screen!) that the compiler is indeed
right!

Not easy. Your students are not happy.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/

```