Version française
Home     About     Download     Resources     Contact us    
Browse thread
Sorted list
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Philippe Wang <lists@p...>
Subject: Re: [Caml-list] Sorted list
skaller wrote:
> On Sat, 2007-08-04 at 12:29 +0200, Philippe Wang wrote:
>   
>> tmp123@menta.net wrote:
>>     
>
>   
>>> Of the standard modules, the most similar seems "set", because allows 
>>> insertion and has the funcion "min_elt". However, the problem is, if 
>>> two timers have the same time, addition of the second one removes the 
>>> first.
>>>
>>> Please, has someone any sugestion?
>>>
>>> Thanks a lot.
>>>       
>> Hello,
>>
>> Let's remind that Set.Make is a functor that takes a module with
>> sig
>>   type t
>>   val compare : t -> t -> int
>> end
>>
>> If you simply give him a compare function that never returns 0, then you 
>> can add multiple elements that are the same.
>>     
>
> You cannot do that! compare function must be a total order.
>   
Why not ?



        Objective Caml version 3.10.0

# include Set.Make(struct type t = int let compare : t -> t -> int = fun 
a b -> match compare a b with 0 -> 1 | n -> n end);;
type elt = int
type t
val empty : t = <abstr>
val is_empty : t -> bool = <fun>
val mem : elt -> t -> bool = <fun>
val add : elt -> t -> t = <fun>
val singleton : elt -> t = <fun>
val remove : elt -> t -> t = <fun>
val union : t -> t -> t = <fun>
val inter : t -> t -> t = <fun>
val diff : t -> t -> t = <fun>
val compare : t -> t -> int = <fun>
val equal : t -> t -> bool = <fun>
val subset : t -> t -> bool = <fun>
val iter : (elt -> unit) -> t -> unit = <fun>
val fold : (elt -> 'a -> 'a) -> t -> 'a -> 'a = <fun>
val for_all : (elt -> bool) -> t -> bool = <fun>
val exists : (elt -> bool) -> t -> bool = <fun>
val filter : (elt -> bool) -> t -> t = <fun>
val partition : (elt -> bool) -> t -> t * t = <fun>
val cardinal : t -> int = <fun>
val elements : t -> elt list = <fun>
val min_elt : t -> elt = <fun>
val max_elt : t -> elt = <fun>
val choose : t -> elt = <fun>
val split : elt -> t -> t * bool * t = <fun>

# let x = empty ;;
val x : t = <abstr>
# let x = add 1 x ;;
val x : t = <abstr>
# let x = add 1 x ;;
val x : t = <abstr>
# let x = add 1 x ;;
val x : t = <abstr>
# let x = add 42 x ;;
val x : t = <abstr>
# let x = add 45 x ;;
val x : t = <abstr>
# elements x ;;
- : elt list = [1; 1; 1; 42; 45]

It works ! ... Or did I miss something ?