我希望使用Haskell生成一组包含26个随机整数的列表,这些整数的和为301。我编写了以下代码:
但它无法编译!我对IO感到困惑!
import System.Random
f 1 sum = [sum]
f n sum = m : (f (n-1) (sum-m))
where m = randomRIO (0,sum)
但它无法编译!我对IO感到困惑!
Occurs check: cannot construct the infinite type: a1 = IO a1
In the first argument of `(:)', namely `m'
In the expression: m : (f (n - 1) (sum - m))
In an equation for `f':
f n sum
= m : (f (n - 1) (sum - m))
where
m = randomRIO (0, sum)