Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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:
> http://malander.undrgnd.net/foobar/students.ml
> 
>   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 students.ml, 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.

e.g.

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

etc...

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);
	done;

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


Good Luck!


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