From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 0E705BBAF for ; Tue, 2 Feb 2010 08:22:44 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ar0CAO9gZ0vBkc3cgWdsb2JhbACGDZU5AQEWJLthhEUE X-IronPort-AV: E=Sophos;i="4.49,389,1262559600"; d="scan'208";a="55124719" Received: from albufera.gva.es ([193.145.205.220]) by mail4-smtp-sop.national.inria.fr with ESMTP; 02 Feb 2010 08:22:43 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AukAAOlhZ0usEGc0/2dsb2JhbAAIhgXRXIRFBA X-IronPort-AV: E=Sophos;i="4.49,389,1262559600"; d="scan'208";a="184832011" Received: from bolonia.gva.es ([172.16.103.52]) by albufera.gva.es with ESMTP; 02 Feb 2010 08:22:42 +0100 Received: (qmail 1139 invoked from network); 2 Feb 2010 07:22:40 -0000 Received: from unknown (HELO [172.17.96.33]) ([172.17.96.33]) (envelope-sender ) by bolonia.gva.es (qmail-ldap-1.03) with SMTP for ; 2 Feb 2010 07:22:40 -0000 Message-ID: <4B67D2AE.6000008@uv.es> Date: Tue, 02 Feb 2010 08:22:22 +0100 From: Miguel Pignatelli Reply-To: pignatelli_mig@gva.es Organization: =?ISO-8859-1?Q?Institut_Cavanilles_-_Universitat_de?= =?ISO-8859-1?Q?_Val=E8ncia?= User-Agent: Thunderbird 2.0.0.19 (X11/20090105) MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: [Caml-list] New to Ocaml References: <20100202152707.8f4a1e24.mle+ocaml@mega-nerd.com> In-Reply-To: <20100202152707.8f4a1e24.mle+ocaml@mega-nerd.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; ocaml:01 pervasives:01 val:01 val:01 ocaml:01 char:01 char:01 wrote:01 wrote:01 ints:01 caml-list:01 immutable:01 'g':01 'g':01 int:01 > let sorted_list = List.sort unsorted_list in .... You should also provide to List.sort a function to compare the elements of the list, the function should have type: 'a -> 'a -> int For basic types, like ints or chars the Pervasives.compare function would serve: # let l = ['g';'b';'f';'c';'a'];; val l : char list = ['g'; 'b'; 'f'; 'c'; 'a'] # let sortedl = List.sort compare l;; val sortedl : char list = ['a'; 'b'; 'c'; 'f'; 'g'] For user-defined types (or if you want to write your own comparison criteria) you should provide the comparison function. For example; # let sortedl = List.sort (fun x y -> if x > y then 1 else 0) [5;3;7;1];; val sortedl : int list = [1; 3; 5; 7] Hope this helps, M; Erik de Castro Lopo wrote: > chaithanya kr wrote: > >> Hi all. I am new to Ocaml. Just started learning recently. >> >> I was studying lists in ocaml. In that, suppose there is a list by name >> singly_list, then by saying "List.length singly_list;;" I will get the >> length of the linked list. >> >> Similarly can anyone tell me as to how to sort a linked list using the >> 'sort' function? Please give me an example of using List.sort. > > In Ocaml, lists are immutable (cannot be changed) and hence, sorting > a list results in a new list: > > let sorted_list = List.sort unsorted_list in .... > > Erik