Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 507
Sudantha Avatar asked Dec 02 '22 02:12

Sudantha


2 Answers

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'
like image 184
Lambdageek Avatar answered Dec 04 '22 01:12

Lambdageek


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,

  • It [IO in this case] could skip the second monadic value, for example when the first one throws an exception.
  • It can pass other data around, in the case of IO a RealWorld state containing open file handles, etc.
  • It can "ensure" that the first one evaluates first, unlike most lambda expression evaluations, where the outermost ("last") terms are expanded first. This is important for print, where the first print needs to change the world first.

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"
like image 38
gatoatigrado Avatar answered Dec 04 '22 03:12

gatoatigrado