Question

would like to print a random number between 0 and 10, but generate seems undefined can not be compiled, the following code edited from example i am using Haskell Platform 2011.2.0.1

Updated:

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

The resulting error:

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
Was it helpful?

Solution

Ok, first problem is that let makes a local binding, and you are using it in global scope, if you want the binding to be local to the main action, I would use where to do the binding. Looking at the QuickCheck docs it appears that the generate function no longer exists. unGen has the same type signature so I believe that has replaced it.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top