我试图得到一个函数,它返回一个零或一列的列表,显然是随机的。
示例:
getBinaryRandomList::Int->Int->[[Int]]
Prelude> getBinaryRandomList 4 3
[[1,0,0,1],[1,1,0,1],[0,0,0,1], [0,1,0,1]]
到目前为止,我已经完成了这个功能:
--tuns an `Int` into a `[Int]`. The [Int] would represent a binary number
int2bin :: Int -> [Int]
int2bin 0 = []
int2bin n = mod n 2 : int2bin (div n 2)
我被困在这里,它在编译时抛出错误:
--returns a random number
import System.Random
randomInt::(Int,Int)->Int
randomInt x y = do
newStdGen
randomR(x, y) getStdGen
...编译
[1 of 1] Compiling Main ( ag.hs, interpreted )
ag.hs:8:25: parse error on input `randomR'
" main"功能会是这样的:
--n lists number
--d digit number
getBinaryRandomList::Int->Int->[[Int]]
getBinaryRandomList d 0 = []
getBinaryRandomList d n = take d (int2bin(randomInt(0,50))) : getBinaryRandomList(n-1)
我的方法是下一步:
randomInt::(Int,Int)->Int
int2bin::Int->[Int] % Already done
getBinaryRandomList::Int->Int->[[Int]]
我怎样才能在Haskell中实现它?