日常吐槽:pure函数式就是蛋疼
我用的是ghc 8.6.5
下面这个会报错:
import System.Random
main = do
g <- getStdGen
let x = take 10 (randomRs (0, 10) g)
print x
错误多多,第一个是:
Ambiguous type variable ��a0�� arising from a use of ��randomRs��
prevents the constraint ��(Random a0)�� from being solved.
Relevant bindings include
x :: [a0] (bound at C:\workbench\haskell\99\random.hs:5:7)
Probable fix: use a type annotation to specify what ��a0�� should be.
改成这样:
import System.Random
main = do
g <- getStdGen
let x = take 10 (randomRs (0, 10) g) :: [Int]
print x
或者换成float也可以,返回10个浮点数。
有趣的是在ghci repl中直接来个block没有这个问题:
:{
do
g<-getStdGen
return $ take 10 (randomRs (0,10) g)
:}
[4,10,0,9,0,3,7,3,5,8]
it :: [Integer]
(0.00 secs, 98,192 bytes)No fuss, 10 integers.
我用的是ghc 8.6.5
下面这个会报错:
import System.Random
main = do
g <- getStdGen
let x = take 10 (randomRs (0, 10) g)
print x
错误多多,第一个是:
Ambiguous type variable ��a0�� arising from a use of ��randomRs��
prevents the constraint ��(Random a0)�� from being solved.
Relevant bindings include
x :: [a0] (bound at C:\workbench\haskell\99\random.hs:5:7)
Probable fix: use a type annotation to specify what ��a0�� should be.
改成这样:
import System.Random
main = do
g <- getStdGen
let x = take 10 (randomRs (0, 10) g) :: [Int]
print x
或者换成float也可以,返回10个浮点数。
有趣的是在ghci repl中直接来个block没有这个问题:
:{
do
g<-getStdGen
return $ take 10 (randomRs (0,10) g)
:}
[4,10,0,9,0,3,7,3,5,8]
it :: [Integer]
(0.00 secs, 98,192 bytes)No fuss, 10 integers.