Pphoenix
Pphoenix

Reputation: 1473

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?

Upvotes: 11

Views: 7126

Answers (1)

MathematicalOrchid
MathematicalOrchid

Reputation: 62808

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

Upvotes: 17

Related Questions