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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:20:00+00:00 2026-05-16T16:20:00+00:00

The followng snippet contains a solution for exercise 3 on page 69 (write a

  • 0

The followng snippet contains a solution for exercise 3 on page 69 (write a function mean to calculate the mean of a list).

While writing some QuickCheck tests to verify whether the its results are more or less sane, I found that on my system (ghc 6.12.3, Haskell Platform 2010.2.0.0 on 32-but Ubuntu 10.4) the tests work for Integer inputs, but not for Int ones. Any idea on why?

import Test.QuickCheck

-- From text and previous exercises
data List a = Cons a (List a)
            | Nil
              deriving (Show)

fromList        :: [a] -> List a
fromList []     = Nil
fromList (x:xs) = Cons x (fromList xs)

listLength             :: List a -> Int
listLength Nil         = 0
listLength (Cons x xs) = 1 + listLength xs

-- Function ``mean`` is the aim of this exercise
mean             :: (Integral a) => List a -> Double
mean Nil         = 0
mean (Cons x xs) = (fromIntegral x + n * mean xs) / (n + 1)
    where n = fromIntegral (listLength xs)

-- To overcome rounding issues
almostEqual     :: Double -> Double -> Bool
almostEqual x y = (abs (x - y)) < 0.000001

-- QuickCheck tests for ``mean``
prop_like_arith_mean :: (Integral a) => [a] -> Property
prop_like_arith_mean xs = not (null xs) ==>
                          almostEqual
                          (mean (fromList xs))
                          (fromIntegral (sum xs) / fromIntegral (length xs))

prop_sum :: (Integral a) => [a] -> Bool
prop_sum xs = almostEqual
              (fromIntegral (length xs) * mean (fromList xs))
              (fromIntegral (sum xs))

-- This passes:
check_mean_ok =
    quickCheck (prop_like_arith_mean :: [Integer] -> Property) >>
    quickCheck (prop_sum :: [Integer] -> Bool)

-- This fails:
check_mean_fail =
    quickCheck (prop_like_arith_mean :: [Int] -> Property) >>
    quickCheck (prop_sum :: [Int] -> Bool)

main = check_mean_ok >>
       check_mean_fail
  • 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-16T16:20:00+00:00Added an answer on May 16, 2026 at 4:20 pm

    Int is based on the underlying system’s int implementation, and will probably the same lower and upper limits as the underlying system (but at least a range of [ -2^29, 2^29 – 1]. Integer has arbitrary precision. Therefore you might be seeing an overflow or underflow when you use Int.

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

Sidebar

Related Questions

Problem There is a program file that contains the following code snippet at some
I got the following snippet ( SomeName / SomeDomain contains real values in my
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
My JSP contains the following snippet: <c:forEach items=${rulesForm.rules} var=rule varStatus=counter> <tr id=rules${counter.index} name=rules[${counter.index}]> rules
Consider the following snippet. qIn is a BlockingQueue , LinkedBlockingQueue implementation while (true) {
Let's consider the following code snippet in Java. There are some of possible approaches
I've just seen this comment on php.net, which contains the following snippet of code:
I have following code snippet: $(document).ready( function() { $(#input1).contextMenu({ menu: 'myMenu' }, function(action, el,
The following html snippet contains an input, a select and a link. When I
My project's copy contains the following snippet in HibernateGrailsPlugin.groovy : def doWithDynamicMethods = {

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.