Accueil     À propos     Téléchargement     Ressources     Contactez-nous

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

[Caml-list] List.sort
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2002-06-19 (14:55) From: Johann Spies Subject: [Caml-list] List.sort
```I apologise for asking a newbie question on this list, but the
beginners-list seems to have a problem.  After 24 hours my message
did not appear on the list and there was actually no activity the last
few days.

I am trying to figure out how to use List.sort.

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
# List.sort (+) k;;
- : int list = [1; 2; 3]
# a;;
- : char list = ['d'; 'b'; 'g']
# List.sort (+) a;;
This expression has type char list but is here used with type int list
# 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
#
---------------------
What is confusing to me is that the manual says:

"Sort a list in increasing order according to a comparison
function. The comparison function must return 0 if it arguments
compare as equal, a positive integer if the first is greater, and a
negative integer if the first is smaller. For example, the compare
function is a suitable comparison function. "

I could not find a single example on how to use this function in the
manual.  Can somebody show me how to use it please?

Johann
--
Johann Spies          Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch

"My son, if sinners entice thee, consent thou not."
Proverbs 1:10
-------------------
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

```