多行if语句Haskell

Pph*_*nix 7 haskell if-statement

我在Haskell中编写一个简单的程序,并具有以下功能:

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
Run Code Online (Sandbox Code Playgroud)

它需要一块板,如果玩家可以根据掷骰子进行移动,则返回新板,否则返回旧板.

但是,haskell不允许我在if语句中使用多行.是否可以使用某种限制器,以便我可以使用letif中的东西?

Mat*_*hid 12

您需要do在每个then/ else分支上重复关键字.

whatever = do
  step1
  step2
  if foo
    then do
      thing1
      thing2
      thing3
    else do
      thing5
      thing6
      thing7
      thing8
Run Code Online (Sandbox Code Playgroud)

  • 或者,您可以说它必须是`if <expr>然后<expr> else <expr>`.OP的格式化不起作用,因为如果没有`do`,行中的三个monadic表达式不会形成有效的表达式. (8认同)
  • 您应该添加为什么需要这样做的原因. (4认同)
  • @bheklilr AFAIK,它需要"因为语言规范如此说明". (3认同)
  • 我会说它更多是因为`if-then-else`的每个子句必须是一个有效的构造(带有作用域)和`thing1; thing2; thing3`不是一个有效的构造,但是`do thing1; thing2; thing3`是. (3认同)