How do I create an arbitrary instance for a recursive datatype?

Viewed 590

I think this creates arbitrary lists of length three, but how do I create lists of arbitrary length?

import Test.QuickCheck

data List a =
  Nil
  | Cons a (List a)
  deriving (Eq, Show)

instance Arbitrary a  => Arbitrary (List a) where
  arbitrary = do
    a <- arbitrary
    a' <- arbitrary
    a'' <- arbitrary
    return $ (Cons a (Cons a' (Cons a'' (Nil))))
2 Answers
Related