Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:49:53+00:00 2026-05-13T11:49:53+00:00

I’m learning Haskell in the hope that it will help me get closer to

  • 0

I’m learning Haskell in the hope that it will help me get closer to functional programming. Previously, I’ve mostly used languages with C-like syntax, like C, Java, and D.

I have a little question about the coding style of an if/else control block used by the tutorial on Wikibooks. The code looks like the following:

doGuessing num = do
   putStrLn "Enter your guess:"
   guess <- getLine
   if (read guess) < num
     then do putStrLn "Too low!"
             doGuessing num
     else if (read guess) > num
            then do putStrLn "Too high!"
                    doGuessing num
            else do putStrLn "You Win!"

It makes me confused, because this coding style totally violates the recommended style in C-like languages, where we should indent if, else if, and else at the same column.

I know it just does not work in Haskell, because it would be a parse error if I indented else at the same column as if.

But what about the following style? I think it is much more clear than the above one. But since the above is used by Wikibooks and Yet Another Haskell Tutorial, which is marked “best tutorial available online” at the official Haskell website, I’m not sure whether this coding style is a convention in Haskell programs.

doGuessing num = do
    putStrLn "Enter your guess:"
    guess <- getLine
    if (read guess) < num then
        do 
            putStrLn "Too low!"
            doGuessing num
        else if (read guess) > num then do 
            putStrLn "Too high!"
            doGuessing num
        else do 
            putStrLn "You Win!"

So, I’m curious about which coding style is used more often—or is there another coding style for this piece of code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:49:54+00:00Added an answer on May 13, 2026 at 11:49 am

    Haskell style is functional, not imperative! Rather than “do this then that,” think about combining functions and describing what your program will do, not how.

    In the game, your program asks the user for a guess. A correct guess is a winner. Otherwise, the user tries again. The game continues until the user guesses correctly, so we write that:

    main = untilM (isCorrect 42) (read `liftM` getLine)
    

    This uses a combinator that repeatedly runs an action (getLine pulls a line of input and read converts that string to an integer in this case) and checks its result:

    untilM :: Monad m => (a -> m Bool) -> m a -> m ()
    untilM p a = do
      x <- a
      done <- p x
      if done
        then return ()
        else untilM p a
    

    The predicate (partially applied in main) checks the guess against the correct value and responds accordingly:

    isCorrect :: Int -> Int -> IO Bool
    isCorrect num guess =
      case compare num guess of
        EQ -> putStrLn "You Win!"  >> return True
        LT -> putStrLn "Too high!" >> return False
        GT -> putStrLn "Too low!"  >> return False
    

    The action to be run until the player guesses correctly is

    read `liftM` getLine
    

    Why not keep it simple and just compose the two functions?

    *Main> :type read . getLine
    
    <interactive>:1:7:
        Couldn't match expected type `a -> String'
               against inferred type `IO String'
        In the second argument of `(.)', namely `getLine'
        In the expression: read . getLine

    The type of getLine is IO String, but read wants a pure String.

    The function liftM from Control.Monad takes a pure function and “lifts” it into a monad. The type of the expression tells us a great deal about what it does:

    *Main> :type read `liftM` getLine
    read `liftM` getLine :: (Read a) => IO a

    It’s an I/O action that when run gives us back a value converted with read, an Int in our case. Recall that readLine is an I/O action that yields String values, so you can think of liftM as allowing us to apply read “inside” the IO monad.

    Sample game:

    1
    Too low!
    100
    Too high!
    42
    You Win!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.