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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:58:53+00:00 2026-06-08T23:58:53+00:00

Let me explain what I mean by a cost-sensitive fold with an example: calculating

  • 0

Let me explain what I mean by a cost-sensitive fold with an example: calculating pi with arbitrary precision. We can use the Leibniz formula (not very efficient, but nice and simple) and lazy lists like this:

pi = foldr1 (+) [(fromIntegral $ 4*(-1)^i)/(fromIntegral $ 2*i+1) | i<-[0..]]

Now, obviously this computation will never complete because we must compute every value in the infinite list. But in practice, I don’t need the exact value of pi, I just need it to some specified number of decimal places. I could define pi’ like this:

pi' n = foldr1 (+) [(fromIntegral $ 4*(-1)^i)/(fromIntegral $ 2*i+1) | i<-[0..n]]

but it’s not at all obvious what value for n I need to pass in to get the precision I want. What I need is some sort of cost-sensitive fold, that will stop folding whenever I achieve the required accuracy. Does such a fold exist?

(Note that in this case it is easy to see if we’ve achieved the required accuracy. Because the Leibniz formula uses a sequence that alternates sign with each term, the error will always be less than the absolute value of the next term in the sequence.)

Edit: It would be really cool to have cost-sensitive folds that could also consider computation time/power consumption. For example, I want the most accurate value of pi given that I have 1 hour of computation time and 10kW-hrs to spend. But I realize this would no longer be strictly functional.

  • 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-06-08T23:58:56+00:00Added an answer on June 8, 2026 at 11:58 pm

    My recommendation is to use a scan instead of a fold. Then traverse the resulting list, until you find the precision you want. A useful special case of the left scan (scanl) is the iterate function:

    piList :: [Double]
    piList =
        map (4*) .
        scanl (+) 0 .
        map recip .
        iterate (\x -> -(x + 2 * signum x)) $ 1
    

    You can now traverse this list. For example you might check when the change to a certain precision becomes invisible:

    findPrec :: (Num a, Ord a) => a -> [a] -> Maybe a
    findPrec p (x0:x1:xs)
        | abs (x1 - x0) <= p = Just x0
        | otherwise          = findPrec p (x1:xs)
    findPrec _ _ = Nothing
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let me explain what I mean in my title. Let's say, that for example
Okay, let's see if I can explain what I mean, here's an image to
First let me explain you what I mean by a nested transaction. Example: say
Let we explain what I mean. Some time ago, while writing a program in
Let me explain what I mean : Let's say we have a menu with
Let me explain what I mean with that question: Lets say I have to
Is this possible? Let me explain what I mean. I have migrated from svn
Let me explain what I mean when I say fluent audio stream. I have
Let me explain what I mean when I say that I want to rotate

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.