【发布时间】:2020-02-26 08:16:36
【问题描述】:
我有随机数生成器
rand :: Int -> Int -> IO Int
rand low high = getStdRandom (randomR (low,high))
和一个从列表中删除元素的辅助函数
removeItem _ [] = []
removeItem x (y:ys) | x == y = removeItem x ys
| otherwise = y : removeItem x ys
我想通过从列表中随机选择一个项目,将其删除并将其添加到列表的前面来对给定列表进行洗牌。我试过了
shuffleList :: [a] -> IO [a]
shuffleList [] = []
shuffleList l = do
y <- rand 0 (length l)
return( y:(shuffleList (removeItem y l) ) )
但无法让它工作。我明白了
hw05.hs:25:33: error: * Couldn't match expected type `[Int]' with actual type `IO [Int]' * In the second argument of `(:)', namely ....
有什么想法吗?
谢谢!
【问题讨论】:
-
在使用结果
return (y:res)之前需要res <- ...递归IO调用。
标签: haskell do-notation