#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Deal a pack of cards


    Hi

    Basically I am trying to write a function which takes a list of cards and deals it out to a given number of people. For example, dealing cards [a,b,c,d,e,f,g] to 3 people would return [[a,d,g],[b,e],[c,f]]. The types are Int -> [a] -> [[a]].

    I can't figure out a way to do this in Haskell. I would have thought to maybe use two loops in Java but is this even possible in Haskell? Another thought I had would be to use modulus values but I can't figure this out either.

    Any ideas would be much appreciated!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    The first approach that comes to mind is: dealing cards 1..N to players A,B,C is the same as dealing cards 2..N to players B,C,A and giving player A card 1.

    Another: dealing cards 1..N to players A,B,C is the same as dealing cards 1,4,7... to player A and dealing the remainder to B,C.

    Are you shuffling the deck? If so there's no need to deal the cards as you would a physical deck, you can get the same effect by giving the first N/3 cards to A, the next N/3 to B, and the rest to C.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0
    Thanks for your reply. The cards have already been shuffled so they need to be dealt in the right order.

IMN logo majestic logo threadwatch logo seochat tools logo