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] Easy solution in OCaml?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-04-26 (14:57)
From: Siegfried Gonzi <siegfried.gonzi@s...>
Subject: Re: [Caml-list] Easy solution in OCaml?
Brian Hurt wrote:

>I find that if you're working against the compiler, either a) you haven't 
>thought the problem through, b) there's an easy solution you're missing, 
>or c) you have a bug.

I think this is a very interesting question. What I love about Scheme 
(or even Python lists) is the fact that you can put into a list what you 
want. I had a problem the other day which was as follows:

I have a Scheme function which extracts floating-point values from 
strings located in a file:


The extracted floating point numbers become stored into an array:

(vector (vector 12.33 43.4 4.56 -1.0 1.23) (vector 23.3 34.4 -1.0 1.2 0.0))

My boss gave me a file which he urgently had to evaluate in order to 
fullfill a time schedule (some measurements from  the field of 
experimental physics). But there was a problem, because the file was as 


The first entry was an annotation but my Scheme function expects a 
string which can be converted to a floating point number. But Scheme is 
your friend here, because one more line in my file-reading function and 
you get something like this:

((name1 2.23 2.334) (name2 3.34 23.2 ...))

In Ocaml I would have to skip the first entry because it is not a 
floating-point value. All my other functions were not affected, because 
passing around arrays or lists does not mean you must put forward 
floating-points or string arrays or whatever.

I agree upon that the above feature can sometimes lead to bad hacks, 
because you the return value of a function can consist of a list where 
you put things into the list which you decide later on whether you want 
augment the list by other parameters, for example:

Your first version of the function has as return value: (list (list 2 3 4))

A year later you decide you want something like this: (list (list 2 3 4) 
(vector 3 4 5) "hi guy")

The goody here is all your other functions which expect the output of 
the above function must not be re-written, as opposed to Clean or OCaml 
where you would have to re-write all your functions because the 
structure of your return-list has changed.

>This is usefull for two reasons, in my experience: sometimes, it lets the 
>compiler produce better code (for example, consider the function:
>let foo x y = x == y
>If you know that x and y will always be integers, doing:
>let foo (x:int) (y:int) = x == y
>allows the compiler to inline integer equals, rather than calling the 
>generic compare function- much faster).
I doubt that type correctness is always better or leads to failure-free 
program execution.  Yesterday I faced the following situation: dividing 
the two vectors:

(vector 0.0 0.0  23.34 23.4)
(vector 0.0 0.0 0.0 23.4)

I forgot to check that division by zero is not a good idea, but the good 
old Bigloo compiler didn't rebel and had as output:

(vector #f #f #f 1.0)

Maybe a bad example because in OCaml you could use exceptions or 
something like this, but in this case the OCaml program had aborted 
(this is also true for C,...). I am not sure how long it would have gone 
good, but the Scheme program had not aborted in a safety-critical 
system-environment. I am really often surprised how forgiving Scheme and 
also CommonLisp actually are in such situations.

S. Gonzi

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