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 7610343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:23:45+00:00 2026-05-31T01:23:45+00:00

Sorry for a poor title, feel free to edit. I can’t understand what the

  • 0

Sorry for a poor title, feel free to edit. I can’t understand what the problem is, so it might be altogether wrong. Below is the code (this is after I’ve done like a hundred of permutations and different sequences of let-do-if and tabulation, and I’m exhausted):

-- The last statement in a 'do' construct must be an expression
numberOfGoods :: IO String
numberOfGoods = do putStrLn "Enter year (2000-2012):\n"
                   let intYear = readYear 
                   in if (intYear < 2000 || intYear > 2012)
                         then error "Year must be withing range: 2000-2012" 
                         else
                                c <- readIORef connection
                                [Only i] <- query_ c ("select count('*')" ++
                                         "from table" ++
                                         "where ((acquisition_date <= " ++
                                         (formatDate intYear) ++
                                         ") and ((sale_date is null) or " ++
                                         "(sale_date < " ++
                                         (formatDate intYear) ++ ")))")
                                return i

readYear :: Integer
readYear = do
           year <- getLine
           read year :: Integer

Something that would meant to be so simple… I still don’t understand what is wrong with the code above. Please, if you could kindly explain the source of the error, that would be great.
I did read about do, let-in and if-then-else, and I don’t see any errors here from what I could understand from the manual.

Ideally, if there are alternatives, I would like very much to reduce the amount of the wasted white space on the left.

Thank you.

  • 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-31T01:23:46+00:00Added an answer on May 31, 2026 at 1:23 am

    readYear is not an Integer, it’s an IO action that can be run to read input and convert the input to an integer — in other words, IO Integer. And as it’s an IO action, you’ll need a return to use whatever read year as result of getYear. That is:

    getYear :: IO Integer
    getYear = do year <- getLine
                 return (read year)
    

    This also means you use it like intYear <- readYear instead of using let (well, you could, but you’d store the IO action instead of running it, and the type of intYear would be wrong). That is:

    numberOfGoods :: IO String
    numberOfGoods = do putStrLn "Enter year (2000-2012):\n"
                       intYear <- readYear
                       ...
    

    do does not extend over if, rather you need to start again with do if you want a sequence of actions in the then or else branch. That is:

                         else
                                c <- readIORef connection
                                ...
                                return i
    

    should be roughly:

                         else do c <- readIORef connection
                                 ...
                                 return i
    

    As for reducing whitespace, consider pushing the validation logic into readYear. Implementing this is left as an exercise to the reader 😉

    As an aside, you don’t need in when using let in a do block (but only there!), you can simply state:

    do do_something
       let val = pure_compuation
       something_else_using val
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the poor title, can't think of a succinct way of putting this..
Sorry for the poor title,I'm new to OOP so I don't know what is
Sorry for the lame title, my descriptive skills are poor today. In a nutshell,
at first sorry about my poor English. Now Im having a problem in closing
Sorry for the poor title guys, but I'm whooped. I have a table as
Sorry for the poor title, I don't really know what to call this. I
Sorry for the poor title, but I couldn't think of another way to describe
Sorry for the poor title. Take these cases: Case 1: a = true; if
Sorry for the poor title, but basically what I am trying to achieve is
Sorry about the poor title, it will be a little easier to describe what

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.