且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数变量不在范围Haskell中

更新时间:2022-10-29 15:17:00

generate isn't a function in System.Random. Perhaps you are looking for next?

EDIT: Let me be clear: I don't know why you are using QuickCheck/Arbitrary for a task that Random/MonadRandom would seem to be more fitting. I'll assume you considered your options and move on.

Must you select your generator? Can't you use sample' :: Gen a -> IO a?

getVal :: IO a
getVal = sample' arbitrary

This should work for QC2.

OTOH, if you really want to use your own StdGen (or want to avoid IO) then try:

import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen

func :: StdGen -> Int
func g = unGen arbitrary g 0

This will use the StdGen named g and a count (0 here,) to generate your value. Because unGen doesn't step the generator, and the counter stepping doesn't give good randomness properties (it seems, you can try and see for yourself) you might end up wanting to wrap this with something that generates StdGens (yuck).

If you don't know what version package you are using then run:

$ ghc-pkg list | grep QuickCheck
(QuickCheck-2.1.1.1)
QuickCheck-1.2.0.1

In my setup (seen above) I have both 1 and 2, but 2 is hidden (the () means hidden) so when I use GHCi and import Test.QuickCheck it's version 1 that I get.

相关阅读

技术问答最新文章