Parse error on input 'if' when trying to use a condition inside a do block

前端 未结 3 2058
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 18:37

I have the following code and I already tried multiple ways to write this, but I can\'t make it work. What I need is to use an if condition inside a do

3条回答
  •  半阙折子戏
    2020-12-20 19:42

    You're missing an else.

    palin :: IO ()
    palin = do
      line <- getLine
      putStr line --Probably better to use putStrLn?
      if True then putStr line else putStr "hi" --Also consider putStrLn here?
    

    You need to provide an value, if you're coming from Python, then think about something like

    a = "dogs" if 1 < 0 else "cats" here, you need to provide the else as without it the variable assignment wouldn't run.

提交回复
热议问题