0

想打印一个 0 到 10 之间的随机数,但生成似乎 undefined 无法编译,下面的代码从我使用 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
4

1 回答 1

2

好的,第一个问题是 let 进行本地绑定,并且您在全局范围内使用它,如果您希望绑定对主要操作是本地的,我会使用 where 进行绑定。查看 QuickCheck 文档,似乎生成功能不再存在。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
于 2011-11-18T07:34:59.057 回答