The following (working) Haskell program outputs a random spell:
import System.Random
spells =
[ "Abracadabra!"
, "Hocus pocus!"
, "Simsalabim!"
]
main :: IO()
main = do
spell <- (spells !!) <$> randomRIO (0, length spells - 1)
putStrLn spell
However, the variable spell
is quite useless. It stores the random string selected from the list of spells, but is then immediately passed to the putStrLn
function and is never used again. I tried to combine the two IO operations into a single line like this:
main = putStrLn <$> (spells !!) <$> randomRIO (0, length spells - 1)
But I got the following error:
• Couldn't match type ‘IO ()’ with ‘()’
Expected type: Int -> ()
Actual type: Int -> IO ()
• In the first argument of ‘(<$>)’, namely
‘putStrLn <$> (spells !!)’
In the expression:
putStrLn <$> (spells !!) <$> randomRIO (0, length spells - 1)
In an equation for ‘main’:
main
= putStrLn <$> (spells !!) <$> randomRIO (0, length spells - 1)
|
160 | main = putStrLn <$> (spells !!) <$> randomRIO (0, length spells - 1)
| ^^^^^^^^^^^^^^^^^^^^^^^^
Is there a way to combine the two IO operations into a single line? I looked at this similar question but I couldn't understand the answer.
(>>=)
is the "canonical" monad operator, as given in Robin Zigmond's answer. However, if you're trying to write code in an applicative-like style, I often enjoy using its flipped version,(=<<)
. It has a nice symmetry with the functions in Functor and Applicative, and how they resemble an ordinary non-monadic function call with just an extra operator interposed:So your expression could be written
Personally I'd rather use more ordinary function composition and less contextual mapping, so I'd move the
(spells !!)
to the left of the bind operator:See how it kinda reads nicely in order this way? "Print out the spell at the index given by
randomRIO (0, length spells - 1)
"?