我在Haskell中写了一个算法来解决子集和问题。签名是
subsetSum :: (Ord a, Num a) => [a] -> a -> Maybe [a]
QuickCheck似乎很适合对此进行测试。例如,我在这里可以检查其中一个属性:
prop_sumEqualsS l s = case subsetSum l s of
Just solution -> (sum solution) == s
Nothing -> True
问题是,该算法计算量很
在以下代码中: data Point = Point { x :: Int,
leftHeight :: Int,
rightHeight :: Int}
data Rectangle = Rec Point Point
rec1 = Rec p1 p2 创建Rec时有几个约束条件,例如: x p1 < x p2
(leftHeight p1 ) should always be zero
(rightHeight p2 ) should always be zero
rig
我对Haskell很陌生,我正试图为Sudoku中的任意单元生成一个随机加权数
90% of the time, it should generate Nothing
10% of the time, it should generate a random value between 1 and 9
这就是我到目前为止想出的(它还没有编译)
-- cell generates an arbitrary cell in a Sudoku
-- cell :: Gen (Maybe Int)
cell = case x of 1 -> return (Just y); _ -> r
我在运行堆栈/GHCi版本8.4.3的macOS上:
在GHCi提示符下和在我的.hs文件中,import Test.QuickCheck都会失败。不管怎样,我被告知它没有被找到。GHCi Prelude>
<no location info>: error:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
在.hs文件中>
"Could not find module ‘Test
在像Haskell的quickcheck这样的基于属性的测试设置中,如何为关系的n元属性生成测试数据,例如,传递性或对称性?我认为,实现语言并不重要。 下面是一个使用rapidcheck的简单C++示例(因为我现在手头就有这个工具): rc::check("Double equality is symmetric.", [](double a, double b) {
RC_ASSERT(!(a == b) || (b == a)); // a == b ==> b == a
}); 在这种天真的情况下,该工具不太可能在前提(a == b)实际成立的情
现在正在通过真实世界的Haskell工作。这是本书中一个非常早的练习的解决方案:
-- | 4) Counts the number of characters in a file
numCharactersInFile :: FilePath -> IO Int
numCharactersInFile fileName = do
contents <- readFile fileName
return (length contents)
我的问题是:你将如何测试这个函数?有没有一种方法可以进行“模拟”输入,而不是真正需要与文件系统交互来测试它?Haskell将重点
我正在使用Haskell和QuickCheck为下列函数编写测试:
{-| Given a list of points and a direction, find the point furthest
along in that direction. -}
fn :: (Eq a, Ord a, DotProd a) => [a] -> a -> a
fn pnts dir = pnts !! index
where index = fromJust $ elemIndex (maximum dotproducts) dotproducts
d
根据的建议,
以及的一些解决方案,
我一直无法使这段代码可行:
instance Semigroup a
=> Semigroup (ZipList a) where
(<>) = liftA2 (<>)
instance Monoid a
=> Monoid (ZipList a) where
mempty = pure mempty
mappend = liftA2 mappend
mconcat as =
foldr mappend mempty as
mconcatP :: NonEmptyL
我尝试通过QuickCheck运行使用Haskell QuickCheck的程序,但是收到了以下错误:
$ ghc Ex2.hs
[1 of 1] Compiling Ex2 ( Ex2.hs, Ex2.o )
Ex2.hs:21:1: error:
Could not find module ‘Test.QuickCheck’
There are files missing in the ‘QuickCheck-2.11.3’ package,
try running 'ghc-pkg check'.
Use -v