2011-11-18 71 views
0

想打印一個介於0和10之間的隨機數,但生成似乎未定義 無法編譯,下面的代碼從示例 編輯了我正在使用Haskell Platform 2011.2.0.1函數在Haskell平臺中生成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 

回答

2

好,第一個問題是,讓使得LOCA l綁定,並且您在全局範圍內使用它,如果您希望綁定對於主要操作是本地的,那麼我將使用綁定的位置。查看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 
+0

變化mkStdGen到MkGen,不能編譯 –

+0

@種瓜得瓜種豆得豆你真的需要一個'StdGen'對象。不支持其他隨機生成器的使用。 – fuz

+0

mkStdGen現在可以使用,但上面不能編譯 –