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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:06:20+00:00 2026-05-30T23:06:20+00:00

I am using this implementation of maybeRead : maybeRead :: (Read a) => String

  • 0

I am using this implementation of maybeRead:

maybeRead :: (Read a) => String -> Maybe a
maybeRead = fmap fst . listToMaybe . filter (null . dropWhile isSpace . snd) . reads

and my own getNum function which prompts until it gets valid input:

getNum :: (Num a, Read a) => String -> IO a
getNum s = do
    putStr s
    input <- fmap maybeRead getLine
    if isNothing input
        then getNum s
        else return $ fromJust input

However if I enter 5.2 it treats it as bad input—Why? There is zero occurences of Int and Integer in my code. I am only using Num, since I want to accept any kind of number.

If I call it explicitly as getNum "Enter a number: " :: IO Double, then it works. Do I have to do this? Is Haskell’s type system just tricking me into thinking I should be able to do this when really it is impossible without full dynamic typing? If so then why does my code even compile; why does it assume integers?

  • 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-30T23:06:21+00:00Added an answer on May 30, 2026 at 11:06 pm

    Your function will indeed accept Integer, Float, or any other Num instance. However, which type it accepts, and by extension how it parses the String, is not determined by the input it receives, it’s determined by what type the result should be based on what you do with it.

    Say you use getNum and pass the resulting value to something that needs a Float; in that case, it will parse a Float value. If you pass it to something that needs an Integer instead, it will parse that.

    As for why it assumes Integer, there is a “defaulting” system for ambiguous types specified in the Haskell Report, and the rules say that an ambiguous type with a Num constraint should default to Integer.

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

Sidebar

Related Questions

i'm using this example implementation found at http://tangentsoft.net/wskfaq/examples/basics/select-server.html This is doing most of what
If I have a service definition/implementation like this: using System; using System.ServiceModel; namespace aspace.service
I'm using this code to parse XML in the instance variable response : @implementation
I have read this implementation of glyphs in the Expert C Programming by Peter
I have readed something about it i wanna to do some implementation using this.
I'm using this as a reference: http://msdn.microsoft.com/en-us/library/dd182449(v=VS.85).aspx So the implementation is very similar. I'm
I'm using the implementation of the algorithm described here: http://www.codezealot.org/archives/55 Using this implementation, when
I am working on a Windows phone 7 application. I am using this implementation
I am using this function for rendering MKMapView instance into image: @implementation UIView (Ext)
I am using this implementation of the ui.slider which controls the ui.tabs as found

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.