Skip to content

Instantly share code, notes, and snippets.

@akulakov
Created October 12, 2014 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akulakov/9e9d18eeb8db6dc23281 to your computer and use it in GitHub Desktop.
Save akulakov/9e9d18eeb8db6dc23281 to your computer and use it in GitHub Desktop.
Bitfunc: recursive, iterative, compare
(define (bitfunc-recur steps x1 x2)
(define (step) (/ (- x2 x1) steps))
(if (> steps 0)
(+ (bitfunc-rect x1 (+ x1 (step))) (bitfunc-recur (- steps 1) (+ (step) x1) x2))
0))
(define (d x) (exact->inexact x))
(define (bitfunc-iter steps x1 x2)
(define step 0)
(set! step (/ (- x2 x1) steps))
(define area 0)
(do ((n 0 (+ n 1))) ((>= n steps))
(set! area (+ area (bitfunc-rect x1 (+ x1 step))))
(set! x1 (+ x1 step))
)
(exact->inexact area))
(define (compare steps x1 x2)
(abs (- (bitfunc-recur steps x1 x2) (bitfunc-iter steps x1 x2)))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment