This post is part of a series of posts on implementing a minimal version of QuickCheck from scratch. The source code is available on GitHub.
In this post I’ll be generating lists of random length.
The maximum length of the list will depend on the runtime size parameter. See this previous post that explains how size works.
Two generators are required for this:
- a generator that picks numbers with absolute value bounded by the size (that’s Gen’s
- a generator that creates lists of a given length (that’s Gen’s
vector). The length will be the number obtained by the above generator
choose is already available. Porting Gen’s
vector, which essentially means porting replicateM
Creating a custom
gen computation expression
Most, if not all, monadic computations on the
Gen<'a> type can be written in terms of
A computation expression just makes this a whole lot easier:
Given the above, a generator for lists of random length can be written as:
Finally, here are some sample lists of type
char, but it can be any generator of type