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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:25:02+00:00 2026-05-28T13:25:02+00:00

I wanted to make a simple average (mean) function in Haskell, so I tried

  • 0

I wanted to make a simple average (mean) function in Haskell, so I tried the following in ghci:

ghci> let avg xs = (sum xs) / (length xs)

And it throws the following error:

No instance for (Fractional Int)
  arising from a use of `/'
Possible fix: add an instance declaration for (Fractional Int)
In the expression: (sum xs) / (length xs)
In an equation for `avg': avg xs = (sum xs) / (length xs)

So, I decided to break it down by trying the following:

ghci> let a = (sum [1,2])
ghci> let b = (length [1,2])

That all works good.

So then I tried the following

ghci> a/b

And I got the following error:

Couldn't match expected type `Integer' with actual type `Int'
In the second argument of `(/)', namely `b'
In the expression: a / b
In an equation for `it': it = a / b

So, in Haskell are Integer and Int different? – And if so, how can I make the original function work?

  • 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-28T13:25:02+00:00Added an answer on May 28, 2026 at 1:25 pm

    The problem is that

    length :: [a] -> Int
    

    but

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

    So, when you say whatever / length xs, it doesn’t type, because Int is not a Fractional number type! This is what the “No instance for…” error is trying to tell you. This definition will work:

    GHCi> let avg xs = sum xs / fromIntegral (length xs)
    

    Here, we use fromIntegral :: (Integral a, Num b) => a -> b to convert the Int we get from length into a Fractional number. Note that the resulting function will only work on lists of Fractional numbers because of this (but e.g. avg [1,2,3] will still work fine).

    To explain the error you get when doing it “in pieces”, it’s because in let a = sum [1,2], the list’s elements are Integers, so their sum is an Integer, but in let b = length [1,2], the resulting length is an Int, per the type of length I showed above. So, when you do a/b, this fails way before it even realises that Int and Integer are not instances of Fractional — since (/) takes two arguments of the same type, it can’t possibly work.

    And yes, Integer and Int are different — Int is a fixed-precision integral type, usually the size of a numeric word, like long in C, whereas Integer is an arbitrary-precision bignum, capable of storing any integer; or at least, any integer that’ll fit into your RAM 🙂

    Another possible approach would be to define avg xs as sum xs / genericLength xs, using Data.List.genericLength, which works with any numeric type, not just Ints:

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

    For that, you’ll have to import Data.List in GHCi. Yet another possible approach (but one resulting in a different function altogether) would be to use integer division: let avg xs = sum xs `div` length xs (note: a `div` b is just div a b; this syntactic sugar works for every function).

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

Sidebar

Related Questions

i wanted to make a simple data entry application. So i did the following
I wanted to make some simple file recovery software, where I want to try
I wanted to make a function that populates a Grid in WPF with pictures.
Let's say I wanted to make a python script interface with a site like
I wanted to make simple form, without awesome div's and CSS - it structure
I'm making an in webpage cache system. I wanted to make a simple page
I wanted to make a simple chart of users that have been created in
I wanted to make a simple R plot with y-axis labels centered above the
I wanted to make a simple Countdown-Application with C# to show as an example.
I just wanted to make sure I don't need Core Data for a simple

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.