;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-reader.ss" "lang")((modname 31.3.1) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp")))))
;; sum : (listof number) -> number
(define (sum L)
(if (empty? L)
0
(+ (car L) (sum (cdr L)))))
;; accu-sum : (listof number) -> number
(define (accu-sum L)
(local ((define (sum L current-sum)
(if (empty? L)
current-sum
(sum (cdr L) (+ current-sum (car L))))))
(sum L 0)))
;; g-series : number -> (listof number)
(define (g-series n)
(if (zero? n)
empty
(cons (expt -0.99 n) (g-series (sub1 n)))))
#| "Depending on which function we use to sum up the items of this list, we get vastly different results. Evaluate the expression
(sum (g-series #i1000))
with both the original version of sum as well as its accumulator-style version. Then evaluate
(* 10e15 (sum (g-series #i1000)))
which proves that, depending on the context, the difference can be arbitrarily large. |#
;; TESTS
(sum (g-series #i1000))
(accu-sum (g-series #i1000))
(* 10e15 (sum (g-series #i1000)))
(* 10e15 (accu-sum (g-series #i1000)))