简体   繁体   English

初学者Haskell,快速检查生成器

[英]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? 我应该如何使用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.. 我想不出一种方法,一次只从列表中取出第一个数字:/也许fmap带有1,我不知道。

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: 让我们首先构造一个生成器,以随机顺序列出具有给定长度的Ord ered对象:

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: 现在,我们可以构建一个Gen爱适易产生每次用一个递增的大小由列表无尽的名单:

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] : 然后,我们可以从这个生成值Gen爱适易与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],...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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