Code Review

Compare your solutions

    #| BEGIN (Введите свое решение) |#
(define (inc x) (+ x 1))

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2)) add-dx b)
     dx))

;(integral cube 0 1 0.01)
; >0.24998750000000042

;(integral cube 0 1 0.001)
; >0.249999875000001

(define (simpson f a b n)
  (define h (/ (- b a) n))
  (define (y k) (f (+ a (* k h))))
  (define (mul k)
    (cond [(or (= k 0) (= k n)) 1.0]
          [(odd? k) 4.0]
          [else 2.0]))
  (define (term k) (* (mul k) (y k)))
  (* (/ h 3) (sum term 0 inc n)))

;(simpson cube 0 1 100)
; >0.24999999999999992

;(simpson cube 0 1 1000)
; >0.2500000000000002

; Simpson’s Rule results are more accurate
#| END |#