yck*_*yck 3 haskell types function
我想编写一个用于构建随机数列表的函数,这里是我编写的代码
buildlist :: Int -> Int -> [Int]
buildlist n m = do
seed <- getStdGen
let l = randomRs (0, m) seed
let list = take n l
return list
Run Code Online (Sandbox Code Playgroud)
然后是错误
Couldn't match expected type `[t0]' with actual type `IO StdGen'
In a stmt of a 'do' block: seed <- getStdGen
In the expression:
do { seed <- getStdGen;
let l = randomRs ... seed;
let list = take n l;
return list }
In an equation for `buildlist':
buildlist n m
= do { seed <- getStdGen;
let l = ...;
let list = ...;
.... }
Run Code Online (Sandbox Code Playgroud)
ps.haskell与c,java,ruby如此不同,我觉得我已经学会了编码
因为您正在使用IO(getStdGen
),所以整个函数必须是IO
monad.将返回类型更改为
buildList :: Int -> Int -> IO [Int]
Run Code Online (Sandbox Code Playgroud)
并且做读一本好书:-)
归档时间: |
|
查看次数: |
702 次 |
最近记录: |