I have a game , user vs computer and I want to randomly choose who starts the game. I have
a = getStdRandom $ randomR (0, 1)
This gets a random number 0 or 1. However it is a IO Int
, so I can't have an if statement comparing it to a number like
if a == 0 then userStarts else computerStarts
I have tried to compare IO Int
with IO Int
and it doesn't work, and I have also tried
I am very new to Haskell, not sure how to approach this. Code details requested:
randomNumber = getStdRandom $ randomR (0, length symbols - 5) -- this will be 0 or 1
randomNumber2 = getStdRandom $ randomR (0, length symbols - 5) -- according to
-- the solution I need another function returning IO int.
a = do
x <- randomNumber
randomNumber2 $ pureFunction x
Error I get:
• Couldn't match expected type ‘t0 -> IO b
with actual type ‘IO Int’
• The first argument of ($) takes one argument,
but its type ‘IO Int’ has none
In a stmt of a 'do' block: randomNumber2 $ pureFunction x
In the expression:
do x <- randomNumber
randomNumber2 $ pureFunction x
• Relevant bindings include
a :: IO b
(bound at Path:87:1)
randomNumber2 $ pureFunction x
Path:89:20: error:
Variable not in scope: pureFunction :: Int -> t0
randomNumber2 $ pureFunction x