* [Caml-list] List.sort
@ 2002-06-19 8:54 Johann Spies
2002-06-19 15:14 ` Stefano Zacchiroli
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Johann Spies @ 2002-06-19 8:54 UTC (permalink / raw)
To: caml-list
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] List.sort
2002-06-19 8:54 [Caml-list] List.sort Johann Spies
@ 2002-06-19 15:14 ` Stefano Zacchiroli
2002-06-19 15:16 ` Henri Dubois-Ferriere
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Stefano Zacchiroli @ 2002-06-19 15:14 UTC (permalink / raw)
To: caml-list
On Wed, Jun 19, 2002 at 10:54:54AM +0200, Johann Spies wrote:
> I am trying to figure out how to use List.sort.
Look at the List.sort type:
('a -> 'a -> int) -> 'a list -> 'a list
the first "argument" required by List.sort have type "function that
takes two arguments of the same type and return an int".
The type of compare is:
'a -> 'a -> int
you then have to pass the function compare itself without applying it
(as I saw in one of your example).
This is a sample usage:
List.sort compare [3;2;1]
Indeed "List.sort compare" have type
'a list -> 'a list
Cheers.
--
Stefano Zacchiroli - undergraduate student of CS @ Univ. Bologna, Italy
zack@cs.unibo.it | ICQ# 33538863 | http://www.cs.unibo.it/~zacchiro
"I know you believe you understood what you think I said, but I am not
sure you realize that what you heard is not what I meant!" -- G.Romney
-------------------
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] List.sort
2002-06-19 8:54 [Caml-list] List.sort Johann Spies
2002-06-19 15:14 ` Stefano Zacchiroli
@ 2002-06-19 15:16 ` Henri Dubois-Ferriere
2002-06-19 15:30 ` sebastien FURIC
2002-06-19 15:26 ` Remi VANICAT
2002-06-19 17:27 ` Alessandro Baretta
3 siblings, 1 reply; 7+ messages in thread
From: Henri Dubois-Ferriere @ 2002-06-19 15:16 UTC (permalink / raw)
To: Johann Spies; +Cc: caml-list
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] List.sort
2002-06-19 8:54 [Caml-list] List.sort Johann Spies
2002-06-19 15:14 ` Stefano Zacchiroli
2002-06-19 15:16 ` Henri Dubois-Ferriere
@ 2002-06-19 15:26 ` Remi VANICAT
2002-06-19 17:27 ` Alessandro Baretta
3 siblings, 0 replies; 7+ messages in thread
From: Remi VANICAT @ 2002-06-19 15:26 UTC (permalink / raw)
To: Johann Spies; +Cc: caml-list
Johann Spies <jspies@sun.ac.za> writes:
> 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.
well, you should have wait longer, I already answer you there.
--
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] List.sort
2002-06-19 8:54 [Caml-list] List.sort Johann Spies
` (2 preceding siblings ...)
2002-06-19 15:26 ` Remi VANICAT
@ 2002-06-19 17:27 ` Alessandro Baretta
2002-06-20 9:29 ` Johann Spies
3 siblings, 1 reply; 7+ messages in thread
From: Alessandro Baretta @ 2002-06-19 17:27 UTC (permalink / raw)
To: Johann Spies; +Cc: caml-list
Johann Spies wrote:
>
> 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 ...
Objective Caml version 3.04+13 (2002-06-05)
# let k = [ 3; 2 ; 1];;
val k : int list = [3; 2; 1]
# List.sort compare k;;
- : int list = [1; 2; 3]
Does this solve your problem? You have to realize that
functions (like Pervasives.compare) are first class citizens
in O'Caml. That is, you can pass a function like List.sort
*another function* as a actual parameter.
This is a fairly elementary question. You should try to
avoid posting questions like this on this list; however, I
am more than willing to lend a hand, insofar as possible, so
you can write to me personally, but please mention "Caml" in
the subject.
Alex
-------------------
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] List.sort
2002-06-19 17:27 ` Alessandro Baretta
@ 2002-06-20 9:29 ` Johann Spies
0 siblings, 0 replies; 7+ messages in thread
From: Johann Spies @ 2002-06-20 9:29 UTC (permalink / raw)
To: caml-list
On Wed, Jun 19, 2002 at 07:27:10PM +0200, Alessandro Baretta wrote:
> This is a fairly elementary question. You should try to
> avoid posting questions like this on this list; however, I
> am more than willing to lend a hand, insofar as possible, so
> you can write to me personally, but please mention "Caml" in
> the subject.
Thanks for all the answers.
Johann
--
Johann Spies Telefoon: 021-808 4036
Informasietegnologie, Universiteit van Stellenbosch
"There is therefore now no condemnation to them which
are in Christ Jesus, who walk not after the flesh, but
after the Spirit." Romans 8:1
-------------------
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
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2002-06-20 9:31 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-06-19 8:54 [Caml-list] List.sort Johann Spies
2002-06-19 15:14 ` Stefano Zacchiroli
2002-06-19 15:16 ` Henri Dubois-Ferriere
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox