Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

randomly sorting an array

19 views
Skip to first unread message

ces...@gmail.com

unread,
Nov 9, 2006, 2:51:53 PM11/9/06
to
I've been trying to understand an error I've been experiencing when
trying to randomly sort an array. I am new with ocaml and not
extremely familiar with it.

-----------

type card_kind = Ace | King | Queen | Jack | Number of int;; (* I know
I can consildate these types.. still learning *)
type card_suit = Club | Diamond | Heart | Spade | Nothing;;
type card = { card: card_kind; suit: card_suit };;

exception Failure;;

let deck = [|
{ card= Ace; suit= Spade },
{ card= Queen; suit= Heart }
|];;

(* Shuffle *)
let shuffle l =
match l with
[||] -> raise Failure
| l -> for x=0 to 100 do
let rand_a = Random.int 2 in
let rand_b = Random.int 2 in
let tmp = l.(rand_a) in
print_int(x);

try
l.(rand_a) <- l.(rand_b);
with Invalid_argument "index out of bounds" -> assert false
| exn -> ();

try
l.(rand_b) <- tmp;
with Invalid_argument "index out of bounds" -> assert false
| exn -> ();

done;

l;
;;

shuffle deck

-----------

With out try:
Fatal error: exception Invalid_argument("index out of bounds")

With try:
File "sortrand2.ml", line 31, characters 55-67:
Fatal error: exception Assert_failure("sortrand2.ml", 24, 55)

If I don't match exn I get this warning:
Warning X: this statement never returns (or has an unsound type.)

Compiling with:
OCAMLRUNPARAM=b ocamlc -g sortrand2.ml -o sortrand2; ./sortrand2

Version:
The Objective Caml toplevel, version 3.09.2

Operating System:
OSX 10.4.x (intel)

A related thread/post:
http://alan.petitepomme.net/cwn/2006.03.07.html#6
http://groups.google.com/group/fa.caml/browse_frm/thread/190ec2f3c3dbac80/a32ea5687ea4a9c0#a32ea5687ea4a9c0

-----------

Hopefully this a common newbie mistake or you can advise me on a better
approach to randomly sort an array...

Clayton

Tim

unread,
Nov 9, 2006, 8:46:14 PM11/9/06
to
You have a comma in your array definition, so Ocaml thinks that your
array is a single-element array of tuples: (card * card).

Change the comma to a Ocaml's list separator, a semicolon, and it
should work fine.

tim

0 new messages