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?
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.
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.
return is actually just a simple function in Haskell. It does not return something. It wraps a value into a monad.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With