[Haskell-cafe] Re: Haskell-Cafe Digest, Vol 69, Issue 38

Tom Lokhorst tom at lokhorst.eu
Tue May 19 05:35:45 EDT 2009


These QuickCheck properties don't really test your sort function.
The `a' type variable will be instantiated to ().

So you will test with lists of units, like so:

ghci> quickCheck (\xs -> isSorted (reverse xs))
OK, passed 100 tests.

This can be simply solved by added a more specific type signature:
isSorted :: [Int] -> Bool

Surly, if the sort function works for Ints, it'll work for any Ord a.

- Tom

> ---------- Forwarded message ----------
> From: Ryan Ingram <ryani.spam at gmail.com>
> To: Eugene Kirpichov <ekirpichov at gmail.com>
> Date: Mon, 18 May 2009 13:57:04 -0700
> Subject: Re: [Haskell-cafe] Haskell in 3 Slides
> On Mon, May 18, 2009 at 11:33 AM, Eugene Kirpichov <ekirpichov at gmail.com> wrote:
>> The main bullet point is missing: Correctness.
>>
>> How could we have forgotten quickcheck?
>>
>>> quickCheck (\xs -> sort (sort xs) == sort xs)
>> OK, 100 tests passed.
>
> I like this, but given that you have a whole slide, I might write this:
>
> isSorted :: Ord a => [a] -> Bool
> isSorted [] = True
> isSorted [x] = True
> isSorted (x:y:rest) = x <= y && isSorted (y:rest)
>
>> quickCheck (\xs -> isSorted (sort xs))
> OK, 100 tests passed.
>
> Or, if you want to lead into a talk about fusion and/or higher order functions:
>
> isSorted [] = True
> isSorted (x:xs) = snd $ foldl' check (head x, True) xs where
>    check (prevElem, restSorted) thisElem = (thisElem, prevElem <=
> thisElem && restSorted)
>
>  -- ryan
>


More information about the Haskell-Cafe mailing list