Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
March 16th, 2010, 04:11 PM
 hels89
Registered User

Join Date: Feb 2010
Posts: 5
Time spent in forums: 1 h 5 m 40 sec
Reputation Power: 0
Haskell - 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
March 16th, 2010, 07:43 PM
 OmegaZero
Contributing User

Join Date: May 2007
Posts: 738
Time spent in forums: 3 Weeks 5 Days 5 h 46 m 27 sec
Reputation Power: 928
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);

#3
March 17th, 2010, 02:16 AM
 hels89
Registered User

Join Date: Feb 2010
Posts: 5
Time spent in forums: 1 h 5 m 40 sec
Reputation Power: 0
Thanks for your reply. The cards have already been shuffled so they need to be dealt in the right order.

 Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Haskell - Deal a pack of cards