多行if语句Haskell
我正在用Haskell编写一个简单的程序,具有以下功能:多行if语句Haskell,haskell,if-statement,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 $
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
它需要一个棋盘,如果玩家可以根据掷骰进行移动,则返回新棋盘,否则返回旧棋盘
但是,haskell不允许我在if语句中使用多行。有没有可能使用某种限制器,这样我就可以在if中使用像
let
这样的东西 您需要在每个分支上重复do
关键字,然后
/else
分支
whatever = do
step1
step2
if foo
then do
thing1
thing2
thing3
else do
thing5
thing6
thing7
thing8
您应该添加之所以需要它的原因。@bheklirafaik,之所以需要它是“因为语言规范这么说”。我想说的更多是因为
if-then-else
的每一个子句都必须是一个独立的有效构造(具有作用域),并且thing1;事物2;thing3
不是一个有效的构造,但是do thing1;事物2;thing3
是。或者,您可以说它必须是,如果是,那么else
。OP的格式设置不起作用,因为如果没有do
,一行中的三个一元表达式就不能构成有效的表达式。样式:“可能要移动的列:”++(show$moves)
可以简化为“可能要移动的列:”+++show moves
。当右边只有一个变量时,不要使用$
:它是多余的。@chi:谢谢!在我发布之前应该使用hlint;)