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

Haskell beginner and QuickCheck

2 views
Skip to first unread message

Vincent Foley

unread,
Mar 5, 2008, 1:33:49 PM3/5/08
to
Hello everybody,

I recently started playing with Haskell, and though I don't entirely
grok concepts such as monads, I try to bulldoze my way through and
hope that by using Haskell long enough, the "eureka" light will turn
on in my head.

As such, I am trying to develop small programs in Haskell for fun. My
latest one is a cribbage point counter. I have successfully written
the library, and from my limited manual tests, it seems to function
well enough. Now however, I would try to push testing further and I
want to see what QuickCheck is all about.

I skimmed over the QuickCheck manual, but I haven't found what I was
looking for. Basically, I want to test my individual counting
functions (count pairs, count straights, count flush, etc.) with a
hand of five card. Here are my type declarations in the library,
followed by how I made Rank and Suit instances of Arbitrary

-- Types
data Suit = Clubs | Diamond | Heart | Spade
deriving (Show, Eq)

data Rank = Ace
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King
deriving (Show, Eq, Enum)

type Card = (Rank, Suit)
type Hand = [Card]

-- Arbitrary instances
instance Arbitrary Suit where
arbitrary = do
suit <- oneof $ map return [Clubs, Diamond, Heart, Spade]
return suit

instance Arbitrary Rank where
arbitrary = do
n <- choose (0, 12)
return (toEnum n)


(Feel free to suggest a better way to do arbitrary if my current code
isn't good.)

Next, I would need a way to generate a Hand, a list of *exactly* five
unique Cards. I am quite unsure about how to do that. Would anyone
be kind enough to help me?

Thank you,

Vincent.

Vincent Foley

unread,
Mar 5, 2008, 1:59:44 PM3/5/08
to
I forgot to mention that for some tests, I will want to control
certain aspects of the hand. For instance, when testing for flushes,
I will want to be able to generate flush hands and non-flush hands.
Is that doable too?
0 new messages