Jump to content

Haskell/Solutions/Indentation

From Wikibooks, open books for an open world

← Back to Indentation

Explicit characters in place of indentation

[edit | edit source]
Exercises

Rewrite this snippet from the Control Structures chapter using explicit braces and semicolons:

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!"

There are of course many valid answers, given that you can indent the code and break the lines however you wish. Here is one of them:

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!";
  };
};