Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Really confused - repost
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-04-04 (03:16)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] Really confused - repost
Ryan Bastic wrote:
> Hey all,
>   Just reread my initial message and realized how incomprehensible it
> was.
>   Basically, what I'm trying to do is on display at:
>   it should be fairly clear what i'm trying to do... i want to split up
> the vector > of student names into varying-sized groups, 

It looks like all your groups are the same size.

 >preferably using arrays of
> arrays, but any other data structure will do :-) 
> if you try executing, you'll see on line 68 there is a
> problem...
> can anyone point me in the direction as to what's going on??

In any case, this looks like a homework assignment, so I'll just give 
you some general advice.

1) Please use descriptive variable names.   Instead of calling a binding 
"x" you might use "number_of_groups".  Everyone will love you.

2) Not everything has to be a reference.  If it doesn't change once it 
is assigned, then a simple let statement will do.


let number_of_students = Array.length students in
let group_size = int_of_string (readline ()) in
let number_of_groups =(number_of_students + group_size - 1)/group_size in


3) Arrays are mutable.  You don't need to make arrays of references in 
order to change the contents of an array.

let tmp = Array.make 1 "" in
let groups = Array.make number_of_groups tmp in


let ar = Array.make !group_size "" in


groups.(!current_group) <- ar;

4) We're programming in a functional language.  I can tell you've 
learned C.  Those are the bad old days.

If you want to iterate over all the students, you should use:

Array.iter (fun student ->
	do something here;
   ) students;

instead of:

	let n = Array.length students - 1 in
	let i = ref 0 in
	while !i <> n do
		do something here with students.(!i);

Not only because it's shorter, but because you're less likely to make a 
mistake (hint, hint).  If you need then array index, look at the iteri 

Good Luck!

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: