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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:04:02+00:00 2026-05-13T21:04:02+00:00

I’m playing around with beginner Haskell, and I wanted to write an average function.

  • 0

I’m playing around with beginner Haskell, and I wanted to write an average function. It seemed like the simplest thing in the world, right?

Wrong.

It seems like Haskell’s type system forbids average from working on a generic numeric type – I can get it to work on a list of Integrals, or an list of Fractionals, but not both.

I want:

average :: (Num a, Fractional b) => [a] -> b
average xs = ...

But I can only get:

averageInt :: (Integral a, Fractional b) => [a] -> b
averageInt xs = fromIntegral (sum xs) / fromIntegral (length xs)

or

averageFrac :: (Fractional a) => [a] -> a
averageFrac xs = sum xs / fromIntegral (length xs)

and the second one seems to work. Until I try to pass a variable.

*Main> averageFrac [1,2,3]
2.0
*Main> let x = [1,2,3]
*Main> :t x
x :: [Integer]
*Main> averageFrac x

<interactive>:1:0:
    No instance for (Fractional Integer)
      arising from a use of `averageFrac ' at <interactive>:1:0-8
    Possible fix: add an instance declaration for (Fractional Integer)
    In the expression: average x
    In the definition of `it': it = averageFrac x

Apparently, Haskell is really picky about its types. That makes sense. But not when they could both be [Num]

Am I missing an obvious application of RealFrac?

Is there way to coerce Integrals into Fractionals that doesn’t choke when it gets a Fractional input?

Is there some way to use Either and either to make some sort of polymorphic average function that would work on any sort of numeric array?

Does Haskell’s type system outright forbid this function from ever existing?

Learning Haskell is like learning Calculus. It’s really complicated and based on mountains of theory, and sometimes the problem is so mindbogglingly complex that I don’t even know enough to phrase the question correctly, so any insight will be warmly accepted.

(Also, footnote: this is based off a homework problem. Everybody agrees that averageFrac, above, gets full points, but I have a sneaking suspicion that there is a way to make it work on both Integral AND Fractional arrays)

  • 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-13T21:04:02+00:00Added an answer on May 13, 2026 at 9:04 pm

    So fundamentally, you’re constrained by the type of (/):

    (/) :: (Fractional a) => a -> a -> a
    

    BTW, you also want Data.List.genericLength

    genericLength :: (Num i) => [b] -> i
    

    So how about removing the fromIntegral for something more general:

    import Data.List
    
    average xs = realToFrac (sum xs) / genericLength xs
    

    which has only a Real constraint (Int, Integer, Float, Double)…

    average :: (Real a, Fractional b) => [a] -> b
    

    So that’ll take any Real into any Fractional.

    And note all the posters getting caught by the polymorphic numeric literals in Haskell. 1 is not an integer, it is any number.

    The Real class provides only one method: the ability to turn a value in class Num to a rational. Which is exactly what we need here.

    And thus,

    Prelude> average ([1 .. 10] :: [Double])
    5.5
    Prelude> average ([1 .. 10] :: [Int])
    5.5
    Prelude> average ([1 .. 10] :: [Float])
    5.5
    Prelude> average ([1 .. 10] :: [Data.Word.Word8])
    5.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From shell.el: If a file ~/.emacs_SHELLNAME' exists, or~/.emacs.d/init_SHELLNAME.sh', it is… May 16, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer Just add depth as a parameter. Then check if its… May 16, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer You can use the tabIndex property and set it to… May 16, 2026 at 8:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.