Google Groups Home
Help | Sign in
randomly sorting an array
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
ces....@gmail.com  
View profile
 More options Nov 9 2006, 2:51 pm
Newsgroups: fa.caml
From: ces....@gmail.com
Date: 9 Nov 2006 11:51:53 -0800
Local: Thurs, Nov 9 2006 2:51 pm
Subject: randomly sorting an array
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/190ec2f3c3db...

-----------

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

Clayton


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim  
View profile
 More options Nov 9 2006, 8:46 pm
Newsgroups: fa.caml
From: "Tim" <timlo...@gmail.com>
Date: 9 Nov 2006 17:46:14 -0800
Local: Thurs, Nov 9 2006 8:46 pm
Subject: Re: randomly sorting an array
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google