【发布时间】:2014-03-20 07:50:57
【问题描述】:
我正在尝试在终端上显示 1 到 7 之间的随机数...
nRandom :: IO ()
nRandom = do
number <- randomRIO (1,7)
putStrLn ("Your random number is: "++show number)
...但是 ghc 没有编译它,我得到如下错误:
No instance for (Random a0) arising from a use of `randomRIO'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Random Bool -- Defined in `System.Random'
instance Random Foreign.C.Types.CChar -- Defined in `System.Random'
instance Random Foreign.C.Types.CDouble
-- Defined in `System.Random'
...plus 33 others
In a stmt of a 'do' block: number <- randomRIO (1, 7)
In the expression:
do { number <- randomRIO (1, 7);
putStrLn ("Your random number is: " ++ show number) }
In an equation for `nRandom':
nRandom
= do { number <- randomRIO (1, 7);
putStrLn ("Your random number is: " ++ show number) }
和,
No instance for (Num a0) arising from the literal `1'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 37 others
In the expression: 1
In the first argument of `randomRIO', namely `(1, 7)'
In a stmt of a 'do' block: number <- randomRIO (1, 7)
谁能告诉我我做错了什么?谢谢;)
【问题讨论】:
标签: haskell random io functional-programming