From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA19671; Wed, 19 Jun 2002 17:20:55 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA19922 for ; Wed, 19 Jun 2002 17:20:54 +0200 (MET DST) Received: from lcavpc41.epfl.ch (lcavpc41.epfl.ch [128.178.8.81]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g5JFKsj10724 for ; Wed, 19 Jun 2002 17:20:54 +0200 (MET DST) Received: from localhost (henridf@localhost) by lcavpc41.epfl.ch (8.11.6/8.11.6) with ESMTP id g5JFGVa01565; Wed, 19 Jun 2002 17:16:31 +0200 X-Authentication-Warning: lcavpc41.epfl.ch: henridf owned process doing -bs Date: Wed, 19 Jun 2002 17:16:31 +0200 (CEST) From: Henri Dubois-Ferriere X-X-Sender: henridf@lcavpc41.epfl.ch Reply-To: Henri DF To: Johann Spies cc: caml-list@inria.fr Subject: Re: [Caml-list] List.sort In-Reply-To: <20020619085454.GA5244@adept.co.za> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk A simple example: # let l = [4; 3; 2; 1];; val l : int list = [4; 3; 2; 1] # List.sort compare l;; - : int list = [1; 2; 3; 4] # List.sort;; - : ('a -> 'a -> int) -> 'a list -> 'a list = you can interpret the signature as follows: 1st argument, of type ('a -> 'a -> int): 'compare' function a function that takes two arguments of identical type, and returns an int. 2nd argument, of type 'a list : list of the *same* type as the arguments of the function passed as the compare function. > According to the manual the Persvasives.compare function is suitable > to use with List.sort. I can't figure out how to use it: > ---------------------------- > # k;; > - : int list = [3; 2; 1] > # List.sort (compare 1 2) k;; > This expression has type int but is here used with type 'a -> 'a -> > int indeed, (compare 1 2) evaluates to an int : # compare 1 2;; - : int = -1 > # List.sort (+) k;; > - : int list = [1; 2; 3] > # a;; this is accepted because (+) takes the right arguments (two ints). Your list is sorted, but that is just by coincidence ;) > - : char list = ['d'; 'b'; 'g'] > # List.sort (+) a;; > This expression has type char list but is here used with type int list As noted above, the second argument passed to List.sort takes a list whose elements must be of same type as the compare function. Here your compare function (+) takes int's, and you are giving a list of char's ... > # List.sort (compare 'a' 'b') a;; > This expression has type int but is here used with type 'a -> 'a -> > int > # compare 'a' 'b';; > - : int = -1 > # > --------------------- same argument as for the first case: (compare 'a' 'b') is a char, not a function. Henri ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners