Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline if statement Haskell

I am writing a simple program in Haskell, and have the following function:

tryMove board player die = do
  let moves = getMoves board player die
  putStrLn ("Possible columns to move: " ++ (show $ moves))

  if not $ null moves then
    let col = getOkInput $ moves
    putStrLn " "
    return $ step board (getMarker player) col firstDie
  else
    putStrLn ("No possible move using "++(show die)++"!")
    return board

It takes a board, and if a player can make a move based on a die roll, the new board is returned, otherwise the old board is returned.

However, haskell won't let me use multiple lines in my if statement. Is it possible to use some kind of limiter so that I can use stuff like let in an if?

like image 439
Pphoenix Avatar asked Dec 15 '14 15:12

Pphoenix


People also ask

How do you do multiple If statements in Haskell?

In Haskell, multiple lines of if will be used by separating each of the if statement with its corresponding else statement. In the above example, we have introduced multiple conditions in one function.

Does Haskell have else if?

As a consequence, the else is mandatory in Haskell. Since if is an expression, it must evaluate to a result whether the condition is true or false, and the else ensures this.

What is return Haskell?

return is actually just a simple function in Haskell. It does not return something. It wraps a value into a monad.


1 Answers

You need to repeat the do keyword on each then/else branch.

whatever = do
  step1
  step2
  if foo
    then do
      thing1
      thing2
      thing3
    else do
      thing5
      thing6
      thing7
      thing8
like image 165
MathematicalOrchid Avatar answered Oct 11 '22 08:10

MathematicalOrchid