[racket] Implementation of Simpson's Rule (SICP Exercise 1.29)

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Nov 6 11:24:54 EST 2013

On Nov 6, 2013, at 7:13 AM, Ben Duan <yfefyf at gmail.com> wrote:

> Thank you, Jens. I didn't know that the inexactness of floating point numbers could make such a big difference.


>From HtDP/1e: 

(define JANUS
  (list #i31
        #i2e+34
        #i-1.2345678901235e+80
        #i2749
        #i-2939234
        #i-2e+33
        #i3.2e+270
        #i17
        #i-2.4e+270
        #i4.2344294738446e+170
        #i1
        #i-8e+269
        #i0
        #i99))



;; [List-of Number] -> Number 
;; add numbers from left to right 
(check-expect (sumlr '(1 2 3)) 6)
(define (sumlr l) 
  (foldl + 0 l))

;; [List-of Number] -> Number 
;; add numbers from right to left 
(check-expect (sumrl '(1 2 3)) 6)
(define (sumrl l) (foldr + 0 l))

Then apply the two functions to JANUS. Enjoy -- Matthias





Posted on the users mailing list.