Search code examples
haskellmonadsif-statementdo-notation

Haskell IF Else


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

Solution

  • 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'