liveBook · Manning

Vector

As explained in Learn Haskell by Example

vector is a QuickCheck generator combinator that produces a list of a specified length using the element type's Arbitrary instance.

vector is a QuickCheck generator combinator that produces a list of a specified length, choosing each element through the element type’s Arbitrary instance. section 9.4.1

Use vector when the test requires:

  • A fixed-length list of randomly generated values. section 9.4.1
  • Automatic selection of the element generator from the requested type. section 9.4.1
  • A contrast with vectorOf, which receives its element generator explicitly. section 9.4.1

Fixed-length test data

A generator such as vector n can create a list of n values when the element type has an Arbitrary instance. This makes it suitable for constructing fixed-size inputs for QuickCheck properties, while vectorOf is the alternative when the element generator must be supplied directly. section 9.4.1

ghci> import Data.Vector as V
ghci> :t V.empty
V.empty :: Vector a
ghci> :t V.cons
V.cons :: a -> Vector a -> Vector a
ghci> V.cons 1 V.empty :: V.Vector Int
[1]
ghci> V.fromList [1..10] :: V.Vector Int
[1,2,3,4,5,6,7,8,9,10]

Do not confuse QuickCheck’s vector generator with Data.Vector, an array-like data structure used for fast indexing and reading. section 13.1.3 section 9.4.1

How Learn Haskell by Example covers it

How deep it goes
Defines vector by its type and behavior, but does not explain its implementation internals.
Examples
Uses fixed-length random lists alongside arbitrary, choose, elements, and custom alphabet generators.
What it assumes
Assumes familiarity with QuickCheck’s Gen type and Arbitrary instances.
What you can do after
Apply it via QuickCheck to generate fixed-size lists without writing an element generator explicitly.
How it connects
Connects vector with QuickCheck, Gen, Arbitrary, vectorOf, listOf, and generator combinators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.