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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:00:19+00:00 2026-05-15T22:00:19+00:00

I have a function (exercise 10.11 in Thompson’s The Craft of Functional Programming )

  • 0

I have a function (exercise 10.11 in Thompson’s The Craft of Functional Programming) which computes an approximation to the value of the definite integral of a function over a domain (a, b). It might not be the most elegant function, but I’m still a a beginner:

import Data.Ratio (Rational, (%), denominator, numerator)
type R = Rational

integrate :: (R -> R) -> R -> (R, R) -> R       
integrate f d (a, b) = foldr (+) 0 $ zipWith (*) (map f [a, a + d..b]) (widths d)
 where widths :: R -> [R]
       widths = \n -> n : widths n

eval :: R -> Double       
eval = \r -> (/) (fromIntegral $ numerator r) (fromIntegral $ denominator r)

For instance,

eval $ integrate (\x -> 20 + x^2) (1%10000) (-3%1, 3%1) = 
 ~> 138.00290001

Now, widths d should be equivalent to the expression [d..]. However, if I replace widths by [d..] in integrate, my function outputs incorrect values. For instance:

integrate' :: (R -> R) -> R -> (R, R) -> R       
integrate' f d (a, b) = foldr (+) 0 $ zipWith (*) (map f [a, a+d..b]) [d..]

eval $ integrate' (\x -> 20 + x^2) (1%10000) (-3%1, 3%1)
 ~> 41400870141.0029

Why is this?

  • 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-15T22:00:20+00:00Added an answer on May 15, 2026 at 10:00 pm

    Because the two statements aren’t equivalent. Consider what happens when I call widths d:

    widths d = d : widths d
             = d : d : widths d
             ...
             = [d, d, d, ...]
    

    In other words, you get an infinite list of ds. However, [d..] returns the list [d, d+1, d+2, ...]. To get an infinite list of ds, you can write [d,d..]; in general, [d,d+n..] creates the infinite list [d, d+n, d+2*n, ...]. More idiomatically, one would generally write repeat d; repeat has the signature a -> [a], and just repeats its argument infinitely.

    Edit: Also, some style, etc., points: function = \x -> ... is the same as function x = ... in all cases. And there’s no particular reason to write your eval function with a prefix /; I’d write it eval r = (fromIntegral $ numerator r) / (fromIntegral $ denominator r); in actual fact, however, I’d just use the fromRational :: Fractional a => Rational -> a function instead of eval. You can also replace foldr (+) 0 with sum. And you don’t need to create an infinite list of ds and then multiply everything; more simply, you could just have sum . map (* d) $ map f [a, a + d..b]. Of course, you can then distribute this out, and have

    integrate'' :: (R -> R) -> R -> (R,R) -> R
    integrate'' f d (a,b) = d * (sum $ map f [a, a+d .. b])`
    

    And then we have

    > fromRational $ integrate'' (\x -> 20 + x^2) (1%10000) (-3%1, 3%1) 
    138.00290001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.