跳转到内容

Haskell/解决方案/缩进

来自维基教科书,开放的书籍,开放的世界

← 返回缩进

使用显式字符代替缩进

[编辑 | 编辑源代码]
练习

使用显式花括号和分号重写控制结构章节中的这段代码

doGuessing num = do
  putStrLn "Enter your guess:"
  guess <- getLine
  case compare (read guess) num of
    LT -> do putStrLn "Too low!"
             doGuessing num
    GT -> do putStrLn "Too high!"
             doGuessing num
    EQ -> putStrLn "You Win!"

当然,有很多有效的答案,因为您可以随意缩进代码并换行。以下是一个示例

doGuessing num = do {
  putStrLn "Enter your guess:";
  guess <- getLine;
  case compare (read guess) num of {
    LT -> do {
      putStrLn "Too low!";
      doGuessing num;
    };
    GT -> do {
      putStrLn "Too high!";
      doGuessing num;
    };
    EQ -> putStrLn "You Win!";
  };
};
华夏公益教科书