[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Chicken segfaults with large-ish inputs
From: |
Nicolas Pelletier |
Subject: |
Re: [Chicken-users] Chicken segfaults with large-ish inputs |
Date: |
Mon, 21 Feb 2011 09:57:46 +0900 |
Hello,
On Sun, Feb 20, 2011 at 06:47, Sam Hardwick <address@hidden> wrote:
>
> (define (sum term a next b)
> (if (> a b)
> 0
> (+ (term a)
> (sum term (next a) next b))))
You can use the following trick to accumulate intermediate results and
thus turn the call to sum into a tail recursive one:
(define (sum term a next b)
(letrec ((helper (lambda (acc t a n b)
(if (> a b)
acc
(helper (+ acc (t a)) t (n a) n b)))))
(helper 0 term a next b)))
With chicken 4.6.0, I get the following:
#;14> (integral cube 0 1 10)
0.25
#;15> (integral cube 0 1 100)
0.25
#;16> (integral cube 0 1 1000)
0.25
#;17> (integral cube 0 1 10000)
0.25
#;18> (integral cube 0 1 100000)
0.249999999999999
#;19> (integral cube 0 1 1000000) ; Used to crash from that point on
0.25
#;20> (integral cube 0 1 10000000)
0.25
Hope this helps,
--
Nicolas