From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id CE4ECBC48 for ; Tue, 29 Mar 2005 10:15:44 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2T8FiAH030685 for ; Tue, 29 Mar 2005 10:15:44 +0200 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 KAA22764 for ; Tue, 29 Mar 2005 10:15:43 +0200 (MET DST) Received: from first.in-berlin.de (dialin-145-254-065-196.arcor-ip.net [145.254.65.196]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j2T8FciN026083 for ; Tue, 29 Mar 2005 10:15:41 +0200 Received: by first.in-berlin.de (Postfix, from userid 501) id C4731C77A7; Tue, 29 Mar 2005 09:14:41 +0200 (CEST) Date: Tue, 29 Mar 2005 09:14:41 +0200 From: Oliver Bandel To: Alex Baretta , caml-list@inria.fr Subject: Re: [Caml-list] Pervasives.compare output type Message-ID: <20050329071441.GA365@first.in-berlin.de> References: <42430B3B.60408@barettadeit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <42430B3B.60408@barettadeit.com> User-Agent: Mutt/1.5.6i X-Miltered: at nez-perce with ID 42490EB0.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42490EAA.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; oliver:01 bandel:01 oliver:01 in-berlin:01 caml-list:01 pervasives:01 baretta:01 pervasives:01 intuitively:01 integers:01 integers:01 subtraction:01 wrote:01 strings:01 strings:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.1 required=5.0 tests=FORGED_RCVD_HELO autolearn=disabled version=3.0.2 X-Spam-Level: On Thu, Mar 24, 2005 at 07:47:23PM +0100, Alex Baretta wrote: > Pervasives.compare currently returns an int. Intuitively it would be > more appropriate for it to return a union type such as the following. > > type comparison_result = Less | Equals | Greater > > What are the reasons behind the present design choice? Maybe it was a historical reason? Didn't comparison functions (in C and many other languages too) throw out an integer value?! Having -1 for smaller (less(er?)) and +1 for bigger (greater) and 0 for equal makes sense at least for integers. let sgn a = match a with _ when a > 0 -> 1 | _ when a < 0 -> -1 | _ -> 0 let compare int_1 int_2 = sgn(int_1 - int_2) As compare normally is a computation that is used to compare integers, but can be more generalized to comparing strings and so on, and maybe because comparing (integers) is derived from the area of mathematics, this historic offset may be the reason for that return value. E.g. comparing strings can be (seems to be) implemented as comparing the integer-values of the characters (index) in the ASCII charset. So, again: that's mathematics of comparing integers. So all in all, there may be these reasons here: - derived from mathematics on integers (and easy implementation to use the sub of two integers for getting the result, which one is greater than the other one, then using the sign of that subtraction) - resulting value is like in other programming languages an integer value Nevertheless it could be done with an output type like you gave above. But you can wrap it, if you want: type comparison_result = Less | Equals | Greater let compare_symbres a b = match (compare a b) with | 1 -> Greater | -1 -> Less | _ -> Equals Ciao, Oliver