[Haskell-cafe] Dealing poker hands, redux

Matthew wonderzombie at gmail.com
Sat Aug 25 08:51:59 CEST 2012


So I've finally got some code which shuffles a deck of cards and deals
out an arbitrary number of hands.

https://gist.github.com/19916435df2b116e0edc

type DealerState = State [Card] [[Card]]

deck :: [Card]
deck = [ (s, r) | s <- suits, r <- ranks ]

shuffleDeck :: Int -> RVar [Card]
shuffleDeck n = shuffle $ concat $ replicate n deck

deal :: Int -> ([[Card]] -> DealerState)
deal n = \xs -> state $ \s -> (xs ++ [take n s], drop n s)

-- |Deal a number of hands a number of cards each.
dealHands :: Int -> Int -> ([[Card]] -> DealerState)
dealHands hs cs = foldr1 (<=<) $ replicate hs (deal cs)

First of all, I have no idea if this is any good. The way I end up
calling dealHands and getting a "real" result is `runState (dealHands
3 7 []) deck`. And I see that I've got nested lambdas all in `deal`.
But hey it took me forever to figure it out and it "works."

I'm using `shuffle` from Data.Random.Extras, which results in an RVar,
and thus the beginning of my perplexity. I'm happy to end up with RVar
inside a State monad, but I'm not sure where to start. To be honest,
I'm only barely understanding what I'm doing with the State monad as
it is. :)

Happy for any help whatsoever!

- Matthew



More information about the Haskell-Cafe mailing list