小编Ser*_*kin的帖子

Haskell IO Int和Int

我最近开始学习Haskell.我正在尝试编写一个选择数组的随机元素的程序:

import System.Random

randomInt :: (Int, Int) -> IO Int
randomInt range = randomRIO range :: IO Int

choseRandom :: [a] -> a
choseRandom list = 
    length list
    >>=
        (\l -> randomInt(0,l-1))
        >>=
            (\num -> (list !! num))

main :: IO ()
main = undefined
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Build FAILED

C:\Users\User\Haskell\Real\src\Main.hs: line 7, column 9:
  Couldn't match expected type `IO Int' with actual type `Int'

    In the return type of a call of `length'

    In the first argument of `(>>=)', namely `length …
Run Code Online (Sandbox Code Playgroud)

monads haskell types io-monad

4
推荐指数
2
解决办法
2894
查看次数

标签 统计

haskell ×1

io-monad ×1

monads ×1

types ×1