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: Thomas Fischbacher <tf@f...>
Subject: Re: [Caml-list] Annoying behaviour of OCaml
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 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

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

> Note that a *specific* ordering was ever gaurenteed- and if I wanted a 
> specific ordering, I should implement it myself.

Well... if we take e.g.:

# [|1;2;3|] < [|4;5|];;
- : bool = false

...how would you implement (non-polymorphic!) general lexicographic
order in OCaml without resorting to C?

-- 
best regards,
Thomas Fischbacher
t.fischbacher@soton.ac.uk