想要打印0到10之间的随机数,但生成似乎未定义 无法编译,下面的代码从例子中编辑 我正在使用Haskell Platform 2011.2.0.1
更新:
import System.IO
import System.Random
import Test.QuickCheck.Function
import Test.QuickCheck.Gen
import Test.QuickCheck
main :: IO()
main = putStrLn (show result)
where result = unGen (choose (0, 10)) (mkStdGen 1) 1
产生的错误:
test6.hs:13:25:
Ambiguous type variable `a0' in the constraints:
(Random a0) arising from a use of `choose' at test6.hs:13:25-30
(Show a0) arising from a use of `show' at test6.hs:12:18-21
(Num a0) arising from the literal `10' at test6.hs:13:36-37
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `unGen', namely `(choose (0, 10))'
In the expression: unGen (choose (0, 10)) (mkStdGen 1) 1
In an equation for `result':
result = unGen (choose (0, 10)) (mkStdGen 1) 1
答案 0 :(得分:2)
好的,第一个问题是让我们创建一个本地绑定,并且你在全局范围内使用它,如果你想让绑定对主操作是本地的,我会用到哪里进行绑定。查看QuickCheck文档,似乎不再存在generate函数。 unGen具有相同的类型签名,所以我相信它已取代它。
import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen
main :: IO ()
main = putStrLn (show result)
where result = unGen (choose (0::Int, 10::Int)) (mkStdGen 1) 1