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
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: 2008-01-10 (20:07)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] Annoying behaviour of OCaml
Zitat von Thomas Fischbacher <>:

> Brian Hurt wrote:
> >>This is indeed unexpected behavior:
> >>    # compare "123" "45";;
> >>    - : int = -1
> >>    # compare [1;2;3] [4;5];;
> >>    - : int = -1
> >>    # compare [|1;2;3|] [|4;5|];;
> >>    - : int = 1
> >>
> > It it?  I mean, all I ever read into compare was that it returned a
> > *consistent* ordering- for example, if a < b and b < c, then a < c.
> Well, yes, this is what the manual says and guarantees. However,
> everybody

Can you prove "everybody"?

Even if this would be the case, not what we expect
is of importance, but what the documentation says.
If there's a difference between documentation and bahaviour,
then there is a bug (in the documentation, or in the
implementation, or both).

> does expect lexicographical ordering here. In particular,
> it would be natural to have
> compare (Array.of_list x) (Array.of_list y) = compare x y

Well, this is one way one could expect it.
But there might be reasons not to do so.
This is mixig two paradigms, and so this might be
used differently.
The same is when using strings: OCaml's strings aren't functional.
You can change them in-place.
This is not following purely functional style, but that's not what
OCaml is prtends to provide.

I normally write my comparison functions by myself, to extract the
elements of a datastructure that should be comapred, but then
use comapre on it (even if other functions might be faster).
If I would compare more complex data structures, I would look into the
documentation. I also was surprised on that language-comparison, but now
slightly remember, this was mentioned on this list before.

Look here,

  compare [|1;2;3;5|] [|1;2;3;4|];;
- : int = 1
# compare [|1;2;3;4|] [|1;2;3;5|];;
- : int = -1

Nice. If the size is equal, it compares "as expected". :-)
Isn't that enough? ;-)
 Now it's clear, how to write your own function ;-)

> As this does not hold (see above), this is an unexpected pitfall.

That's your opinion.
There might be other opinions on it.

I would say, it's a design-decision.
And apparantly different then you would do it.