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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:55+00:00 2026-05-26T18:39:55+00:00

I’m trying to write a program in Haskell that gets a list (of integer)

  • 0

I’m trying to write a program in Haskell
that gets a list (of integer) and prints out the number of elements that are bigger than the list’s average

So far I tried

getAVG::[Integer]->Double
getAVG x = (fromIntegral (sum x)) / (fromIntegral (length x))
smallerThanAVG:: [Integer]->Integer
smallerThanAVG x = (map (\y -> (if (getAVG x > y) then 1 else 0)) x)

For some reason I’m getting this error

   Couldn't match expected type `Double'
           against inferred type `Integer'
      Expected type: [Double]
      Inferred type: [Integer]
    In the second argument of `map', namely `x'

It could be that I haven’t written the logic correctly, although I think I did..
Ideas?

  • 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-26T18:39:56+00:00Added an answer on May 26, 2026 at 6:39 pm

    These errors are the best kind, because they pinpoint where you have made a type error.

    So let’s do some manual type inference. Let’s consider the expression:

    map (\y -> (if (getAvg x > y) then 1 else 0)) x
    

    There are a few constraints we know off the bat:

    map    :: (a -> b) -> [a] -> [b]  -- from definition
    (>)    :: Num a => a -> a -> Bool -- from definition
    getAvg :: [Integer] -> Double     -- from type declaration
    1, 0   :: Num a => a              -- that's how Haskell works
    x      :: [Integer]               -- from type declaration of smallerThanAVG
    

    Now let’s look at the larger expressions.

    expr1 = getAvg x
    expr2 = (expr1 > y)
    expr3 = (if expr2 then 1 else 0)
    expr4 = (\y -> expr3)
    expr5 = map expr4 x
    

    Now let’s work backwards. expr5 is the same as the RHS of smallerThanAVG, so that means it has the same result type as what you’ve declared.

    expr5 :: Integer -- wrong
    

    However, this doesn’t match our other constraint: the result of map must be [b] for some b. Integer is definitely not a list (although if you get facetious, it could be coerced into a list of bits). You probably meant to sum that list up.

    expr6 = sum expr5
    
    sum :: Num a => [a] -> a
    

    Now let’s work forwards.

    expr1 :: Double -- result type of getAvg
    y :: Double     -- (>) in expr2 requires both inputs to have the same type
    expr4 :: (Integer -> [a]) -- because for `map foo xs` (expr5)
                              -- where xs :: [a], foo must accept input a
    y :: Integer    -- y must have the input type of expr4
    

    Herein lies the conflict: y cannot be both a Double and an Integer. I could equivalently restate this as: x cannot be both a [Double] and [Integer], which is what the compiler is saying. So tl;dr, the kicker is that (>) doesn’t compare different types of Nums. The meme for this sort of problem is: “needs more fromIntegral“.

    (getAvg x > fromIntegral y)
    
    • 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 ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
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'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 need a function that will clean a strings' special characters. I do NOT

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.