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

  • Home
  • SEARCH
  • 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 4018112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:56:34+00:00 2026-05-20T09:56:34+00:00

In my Haskell program, I want to read in a value given by the

  • 0

In my Haskell program, I want to read in a value given by the user using the getLine function. I then want to use the read function to convert this value from a string to the appropriate Haskell type. How can I catch parse errors thrown by the read function and ask the user to reenter the value?

Am I right in thinking that this is not an “IO Error” because it is not an error caused by the IO system not functioning correctly? It is a semantic error, so I can’t use IO error handling mechanisms?

  • 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-20T09:56:35+00:00Added an answer on May 20, 2026 at 9:56 am

    You don’t want to. You want to use reads instead, possibly like that:

    maybeRead = fmap fst . listToMaybe . reads
    

    (though you might want to error out if the second element of the tuple is not "", that is, if there’s a remaining string, too)

    The reason why you want to use reads instead of catching error exceptions is that exceptions in pure code are evil, because it’s very easy to attempt to catch them in the wrong place: Note that they only fly when they are forced, not before. Locating where that is can be a non-trivial exercise. That’s (one of the reasons) why Haskell programmers like to keep their code total, that is, terminating and exception-free.

    You might want to have a look at a proper parsing framework (e.g. parsec) and haskeline, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following Haskell program prompts the user for a password in the terminal and
I want to make a haskell program where some shape is drawn in a
I'm trying to learn Haskell and want to write a small program which prints
From the other languages I program in, I'm used to having ranges. In Python,
I'm using lots of different records in a program, with some of them using
Apologies in advance if this question is a bit vague. It's the result of
When all you have is a pair of bolt cutters and a bottle of
I spent the last 18 months getting the grip of functional programming, starting with
I can write both Prim's and Kruskal's algorithms to find a minimum spanning tree
Got new project in my TODO and can't chose F# or Nemerle. I'm currently

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.