生成不等于 Haskell 中指定整数的整数列表?

Generating a list of integers that isn't equal to a specified integer in Haskell?

是否有修饰符允许我生成不包含指定整数的整数列表?

这是一个完成相同工作的函数:

listofInts :: Int -> Gen [Integer]
listofInts  a = rmInt a [] arbitrary

rmInt :: Int -> [Int] -> [Int] -> [Int]
rmInt a newList [] = newList
rmInt a newList (x:xs)
|a == x = newList : rmInt a xs
|otherwise = newList : x : rmInt a xs

我假设您在写 Gen [Integer] 时实际上是指 Gen [Int]。您可以简单地过滤生成的列表:

listofInts :: Int -> Gen [Int]
listofInts x = filter (/= x) <$> arbitrary

来自 Test.QuickCheck.GenlistOfsuchThat 组合器应该能让你做到这一点。

listofInts :: Int -> Gen [Integer]
listOfInts x = listOf (suchThat arbitrary (/=x))

这种方法的优点是更尊重生成的大小参数:suchThat 确保与谓词不匹配的生成值不会影响大小。