caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: Alex Baretta <alex@barettadeit.com>, caml-list@inria.fr
Subject: Re: [Caml-list] Pervasives.compare output type
Date: Tue, 29 Mar 2005 09:14:41 +0200	[thread overview]
Message-ID: <20050329071441.GA365@first.in-berlin.de> (raw)
In-Reply-To: <42430B3B.60408@barettadeit.com>

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


  parent reply	other threads:[~2005-03-29  8:15 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-24 18:47 Alex Baretta
2005-03-24 19:41 ` [Caml-list] " Richard Jones
2005-03-24 21:00   ` Marcin 'Qrczak' Kowalczyk
2005-03-24 21:38     ` Bardur Arantsson
2005-03-24 22:07       ` [Caml-list] " Jason Hickey
2005-03-24 22:26         ` brogoff
2005-03-25  9:42         ` Alex Baretta
2005-04-01  5:59           ` Aleksey Nogin
2005-03-24 22:15       ` Marcin 'Qrczak' Kowalczyk
2005-03-24 22:41         ` Bardur Arantsson
2005-03-25  9:43         ` [Caml-list] " Alex Baretta
2005-03-29  7:14 ` Oliver Bandel [this message]
2005-03-30 14:17 ` [Caml-list] " Xavier Leroy
2005-03-30 14:45   ` Alex Baretta
2005-03-30 15:11     ` Jacques Carette
2005-03-30 15:28       ` Alex Baretta
2005-03-30 17:47       ` brogoff
2005-03-30 18:21         ` Jacques Carette
2005-03-30 18:49           ` brogoff
2005-03-30 20:06             ` Jon Harrop
2005-03-30 20:43               ` Jacques Carette
2005-03-30 22:14                 ` Christopher Dutchyn
2005-03-31  0:44                 ` brogoff
2005-03-30 22:43             ` GADT?? (Re: [Caml-list] Pervasives.compare output type) Oliver Bandel
2005-03-30 22:35     ` [Caml-list] Pervasives.compare output type Oliver Bandel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050329071441.GA365@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=alex@barettadeit.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).