From: Henri Dubois-Ferriere <henridf@lcavsun1.epfl.ch>
To: Johann Spies <jspies@sun.ac.za>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] List.sort
Date: Wed, 19 Jun 2002 17:16:31 +0200 (CEST) [thread overview]
Message-ID: <Pine.LNX.4.44.0206191702290.32634-100000@lcavpc41.epfl.ch> (raw)
In-Reply-To: <20020619085454.GA5244@adept.co.za>
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 = <fun>
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
next prev parent reply other threads:[~2002-06-19 15:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-06-19 8:54 Johann Spies
2002-06-19 15:14 ` Stefano Zacchiroli
2002-06-19 15:16 ` Henri Dubois-Ferriere [this message]
2002-06-19 15:30 ` sebastien FURIC
2002-06-19 15:26 ` Remi VANICAT
2002-06-19 17:27 ` Alessandro Baretta
2002-06-20 9:29 ` Johann Spies
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=Pine.LNX.4.44.0206191702290.32634-100000@lcavpc41.epfl.ch \
--to=henridf@lcavsun1.epfl.ch \
--cc=caml-list@inria.fr \
--cc=henri.dubois-ferriere@epfl.ch \
--cc=jspies@sun.ac.za \
/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