-- 3 (find k"th element of a list)
element_at xs x = xs !! x
prop_3a xs x = (x < length xs && x >= 0) ==> element_at xs (x::Int) == (xs !! x::Int)
When prop_3a is ran through QuickCheck, it gives up, because it won’t generate long enough lists.
How can I write a generator that will generate lists with length longer than the random integer?
How about going the other way? First we let QuickCheck pick a list and then we constrain what indices we allow. This works, and does not throw away any test cases.
Here, I use
forAllto use a specific generator for the indices, in this case usingchoosewhich picks an element from a specified range, and I also use theNonEmptyListtype to ensure that we don’t try to index into an empty list.