最近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
次のエラーが表示されます。
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 list'
In the first argument of `(>>=)', namely
`length list >>= (\ l -> randomInt (0, l - 1))'
私が間違っていることは何ですか?初めてモナドを扱うのは難しい