简体   繁体   中英

Beginner Haskell, quickcheck generator

I want to generate an increasing number of lists ie

 prelude>sample' incList
 [[],[19],[6,110],[24,67,81]....]

How should I use vectorOf?

 incList:: Gen [Integer]
 incList=   
 do x<-vectorOf [0..] arbitrary
    return x

I cant think of a way to just take out the first number from the list one at a time :/ Maybe something with fmap take 1, I dunno..

I think you here aim to do too much at once. Let us first construct a generator for a random list of Ord ered objects with a given length in ascending order:

import Data.List(sort)

incList :: (Arbitrary a, Ord a) => Int -> Gen [a]
incList n = fmap sort (vectorOf n arbitrary)

Now we can construct a Gen erator that generates an endless list of lists by each time incrementing the size with one:

incLists :: (Arbitrary a, Ord a) => Gen [[a]]
incLists = mapM incList [0..]

We can then generate values from this Gen erator with generate :: Gen a -> IO [a] :

Prelude File> generate incLists :: IO [[Int]]
[[],[-19],[6,25],[-19,-14,15],[-4,6,20,28],[-23,-19,-6,-1,22],[-29,-21,-13,-9,-9,15],[-23,-15,-4,3,3,27,27],[-29,-29,-26,-25,18,19,23,27],[-24,-23,-16,-14,0,13,17,17,23],[-29,-15,-12,-4,-1,1,2,20,22,26],[-26,-24,-22,-16,-12,5,5,10,11,25,29],[-29,-28,-20,-14,-9,-7,-3,14,15,20,26,28],...]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM