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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:05:07+00:00 2026-06-07T19:05:07+00:00

So I’ve been enjoying this challenging language, I am currently working on an assignment

  • 0

So I’ve been enjoying this challenging language, I am currently working on an assignment for school.

This is what it says: I need to prompt the user for a list of numbers, then display the average of the list , I am so close to figuring it out. However I get this weird parse error:

"Exception: user error (Prelude.readIO: no parse)" 

Here is my code:

module Main (listM', diginums', getList, main) where 

import System.IO     
import Data.List

diginums' = []
listM' = [1, 2, 3]
average' = (sum diginums') / (fromIntegral (length diginums'))
getList :: IO [Double]
getList = readLn

main = do
      putStrLn "Please enter a few numbers" 
      diginums' <- getList
      putStrLn $ show average' 

Terminal Prompts : Enter a few #'s

I Enter : 123

ERROR : Exception: user error (Prelude.readIO: no parse)

I know my functions are working correctly to calculate the average. Now I think my problem is that when I take in the list of numbers from the user, I don’t correctly parse them to type Double for my average function.

  • 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-06-07T19:05:08+00:00Added an answer on June 7, 2026 at 7:05 pm

    Your type signature says that

    getList :: IO [Double]
    getList = readLn
    

    reads a list of Doubles, that means it expects input of the form

    [123, 456.789, 1011.12e13]
    

    but you gave it what could be read as a single number, "123". Thus the read fails, the input couldn’t be parsed as a [Double].

    If you want to parse input in a different form, not as syntactically correct Haskell values, for example as a space-separated list of numbers, you can’t use readLn, but have to write a parser for the desired format yourself. For the mentioned space-separated list of numbers, that is very easy, e.g

    getList :: IO [Double]
    getList = do
        input <- getLine
        let nums = words input
        return $ map read nums
    

    If you want to get the list in the form of numbers each on its own line, ended by an empty line, you’d use a recursion with an accumulator,

    getList :: IO [Double]
    getList = completeList []
    
    completeList :: [Double] -> IO [Double]
    completeList acc = do
        line <- getLine
        if null line
            then return (reverse acc)
            else completeList (read line : acc)
    

    Parsers for more complicated or less rigid formats would be harder to write.

    When the parsing is fixed, you run into the problem that you haven’t yet got used to the fact that values are immutable.

    diginums' = []
    listM' = [1, 2, 3]
    average' = (sum diginums') / (fromIntegral (length diginums'))
    

    defines three values, the Double value average' is defined in terms of the empty list diginums', hence its value is

    sum diginums' / fromIntegral (length diginums') = 0 / 0
    

    which is a NaN.

    What you need is a function that computes the average of a list of Doubles, and apply that to the entered list in main.

    average :: [Double] -> Double
    average xs = sum xs / fromIntegral (length xs)
    
    main = do
        putStrLn "Please enter a few numbers" 
        numbers <- getList
        print $ average numbers
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.