Version française
Home     About     Download     Resources     Contact us    
Browse thread
Annoying behaviour of OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Berke Durak <berke.durak@e...>
Subject: Re: [Caml-list] Annoying behaviour of OCaml
Thomas Fischbacher a écrit :
> 
> Yesterday, we were bitten quite badly by a rude surprise where OCaml
> behaved in a rather different way from what we would have expected:

Surprising.  I didn't notice before that arrays are compared 
hierarchically and not lexicographically.

> I think it would be a *very* good idea to provide
> compare_lexicographically in addition to compare as a pre-defined built-in,
> as this is (1) what many people want to do, (2) it cannot be implemented
> without resorting to either C or black magic, and (3) it would not change
> the behaviour of already existing code that uses "compare".

Lexicographic order is quite natural for recursive sum types such as lists.

As for arrays, I guess the intent was to accelerate cases where the
array lengths are different.  Assume you are sorting a random array of
arrays of random lengths, but whose contents have large common prefixes. 
  You'll have to do more work to sort these with lexicographical ordering.

OTOH this reasoning would also apply to strings which are compared 
lexicographically...  Ocaml's ways are mysterious!

However I don't think it's worth cluttering the standard library with
compare_lexicographically as it can be easily implemented.
--
Berke DURAK