问题描述
您好我有以下代码
不是 code>。Per你正在寻找 next ?
编辑:
让我明确:我不知道为什么你使用QuickCheck / Arbitrary来执行Random / MonadRandom似乎更合适的任务。我会假设你考虑了你的选择并继续前进。
你必须选择你的发电机吗?你不能使用 sample':: Gen a - > IO a ?
getVal :: IO a
getVal = sample'arbitrary
这应该适用于QC2。如果你真的想使用你自己的 StdGen (或者想避免IO),那么试试:
/ p>
import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen
func :: StdGen - > Int
func g = unGen任意g 0
这将使用 StdGen 命名为 g 和一个计数( 0 在这里)来生成您的值。因为unGen不会生成生成器,并且计数器步进不能提供良好的随机性(看起来,您可以尝试自己查看),您最终可能希望用产生 StdGen s(yuck)。
如果您不知道您使用的是哪个版本的软件包,请运行:
$ ghc-pkg list | grep QuickCheck
(QuickCheck-2.1.1.1)
QuickCheck-1.2.0.1
在我的设置中(见上),我有1和2,但隐藏2(()意味着隐藏),所以当我使用GHCi并导入 Test.QuickCheck 它是我得到的版本1.
Hi i have the following code
import Data.Maybe import Test.QuickCheck import System.Random rndExpr :: Gen Expr -> IO Expr rndExpr gen = do rnd <- newStdGen return (generate 5 rnd gen)But i get "not in scope "generate", why is this so?
RegardsDarren
Edit i am importing Test.QuickCheck but it still complaints about the "generate" is not in scope.
Edit 2
How would you write this function so that it would work with quickcheck version 2? I simple tried to put "unGen" where generate was with no succsess, i also installed quickcheck v 2 (cabal install QuickCheck-2.1.0.3)
I need a function with following properties stdGen->Gen Expr->Expr'and unGen seem to give me that functionality, but as I said, my compiler cant find that function. Are there any other functions that I could use for this problem?
解决方案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' arbitraryThis 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 0This 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.1In 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.
这篇关于函数变量不在范围Haskell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!