函数变量不在范围Haskell中 [英] Function variable not in scope Haskell

查看:620
本文介绍了函数变量不在范围Haskell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有以下代码

  import Data.Maybe 
import Test.QuickCheck
import System.Random


rndExpr :: Gen Expr - > IO Expr
rndExpr gen = do
rnd< - newStdGen
return(generate 5 rnd gen)

但是我得到了不在范围内生成,为什么会这样?



问候
Darren



编辑我正在导入Test.QuickCheck,但它仍然抱怨生成不在范围内。



编辑2



你如何编写这个函数,以便它可以与quickcheck版本2一起工作?我简单地试图把unGen放在没有succsess的地方,我还安装了quickcheck v 2(cabal install QuickCheck-2.1.0.3)



我需要一个具有以下属性的函数 stdGen-> Gen Expr-> Expr'
和unGen似乎给了我这个功能,但正如我所说的,我的编译器无法找到该函数。是否还有其他函数可用于解决此问题?

解决方案 不是 System.Random中的函数 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?

Regards Darren

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' 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.

这篇关于函数变量不在范围Haskell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆