input <- readLn
if (input == 0)
then
putStr "0"
else if (input ==1)
then
putStr "1"
else if (input ==2)
in this kind of senario how to use multiple putStr with in a then
or else if
?
when i try getting a error
Type error in application
*** Expression : putStr "0" putStr "0"
*** Term : putStr
*** Type : String -> IO ()
*** Does not match : a -> b -> c -> d
Use do
-notation:
do
a <- something
if a
then
do
cmd1
cmd2
else
do
cmd3
cmd4
cmd5 -- this comes after the 'then' and the 'else'
The canonical explanation for this is that you want to form a new monadic value out of two existing ones. Let's look at the type of putStr,
IO ()
That means it's some black box, that when executed, will "return" the (one-and-only) value of unit type. The key idea behind monadic computation is that you have a combinator >>=
which will put together two monadic expressions, feeding the result of one into the next (more accurately, a function that creates the next). One critical point is that the IO
library provides this combinator, meaning that,
IO
a RealWorld
state containing open file handles, etc.In your case, use it like this,
putStr "0" >>= (\c -> putStr "0")
There's a shortcut, of course,
putStr "0" >> putStr "0"
and the do-notation, as mentioned by another poster, which is yet more syntax sugar,
do
putStr "0"
putStr "0"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With