caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* New to Ocaml
@ 2010-02-02  4:18 chaithanya kr
  2010-02-02  4:27 ` [Caml-list] " Erik de Castro Lopo
  2010-02-02  8:58 ` Richard Jones
  0 siblings, 2 replies; 5+ messages in thread
From: chaithanya kr @ 2010-02-02  4:18 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 391 bytes --]

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.

Thank you

Bye
Chaitanya

[-- Attachment #2: Type: text/html, Size: 442 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] New to Ocaml
  2010-02-02  4:18 New to Ocaml chaithanya kr
@ 2010-02-02  4:27 ` Erik de Castro Lopo
  2010-02-02  7:22   ` Miguel Pignatelli
  2010-02-02  8:58 ` Richard Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Erik de Castro Lopo @ 2010-02-02  4:27 UTC (permalink / raw)
  To: caml-list

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
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] New to Ocaml
  2010-02-02  4:27 ` [Caml-list] " Erik de Castro Lopo
@ 2010-02-02  7:22   ` Miguel Pignatelli
  0 siblings, 0 replies; 5+ messages in thread
From: Miguel Pignatelli @ 2010-02-02  7:22 UTC (permalink / raw)
  To: caml-list

 >    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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] New to Ocaml
  2010-02-02  4:18 New to Ocaml chaithanya kr
  2010-02-02  4:27 ` [Caml-list] " Erik de Castro Lopo
@ 2010-02-02  8:58 ` Richard Jones
  2010-02-02 13:04   ` chaithanya kr
  1 sibling, 1 reply; 5+ messages in thread
From: Richard Jones @ 2010-02-02  8:58 UTC (permalink / raw)
  To: chaithanya kr; +Cc: caml-list

On Mon, Feb 01, 2010 at 11:18:07PM -0500, chaithanya kr wrote:
> Hi all. I am new to Ocaml. Just started learning recently.

There's also a beginners group for learning OCaml:

Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

Rich.

-- 
Richard Jones
Red Hat


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] New to Ocaml
  2010-02-02  8:58 ` Richard Jones
@ 2010-02-02 13:04   ` chaithanya kr
  0 siblings, 0 replies; 5+ messages in thread
From: chaithanya kr @ 2010-02-02 13:04 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 413 bytes --]

I am thankful to all of you.

Bye
Chaitanya

On Tue, Feb 2, 2010 at 3:58 AM, Richard Jones <rich@annexia.org> wrote:

> On Mon, Feb 01, 2010 at 11:18:07PM -0500, chaithanya kr wrote:
> > Hi all. I am new to Ocaml. Just started learning recently.
>
> There's also a beginners group for learning OCaml:
>
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
> Rich.
>
> --
> Richard Jones
> Red Hat
>

[-- Attachment #2: Type: text/html, Size: 877 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-02-02 13:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-02  4:18 New to Ocaml chaithanya kr
2010-02-02  4:27 ` [Caml-list] " Erik de Castro Lopo
2010-02-02  7:22   ` Miguel Pignatelli
2010-02-02  8:58 ` Richard Jones
2010-02-02 13:04   ` chaithanya kr

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).