(define (sum term a next b) (define (iter term a next b result) (if (> a b) (iter ( (iter term a next b 0))
(define (product term a next b) (if (> a b) 1 (* (term a) (product term (next a) next b)))) (define (factorial x) (define (inc n) (+ n 1)) (define (self x) x) (product self 1 inc x)) (factorial 10) (define (pi-over-four a b) (define (term x) (if (= 1 (remainder x 2)) (/ (+ x 1) (+ x 2)) (/ (+ x 2) (+ x 1)))) (define (next x) (+ x 1)) (product term a next b)) (* 4 (pi-over-four 1 10))
(define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) (define (product term a next b) (accumulate * 1 term a next b)) (define (factorial x) (define (inc n) (+ n 1)) (define (self x) x) (product self 1 inc x)) (factorial 10) (define (sum term a next b) (accumulate + 1 term a next b)) (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (define (square x) (* x x)) (integral square 0 1 0.001)
0.33433325000000047
(define (filter-accumulate combiner null-value filter term a next b) (cond ((> a b) null-value) ((filter a) null-value) (else (combiner (term a) (accumulate combiner null-value term (next a) next b))))) (define (sum-filter filter term a next b) (filter-accumulate + 0 filter term a next b)) (define (sum-prime-squares a b) (define (square x) (* x x)) (define (inc x) (+ 1 x)) (sum-filter prime? square a inc b))
suppose we define
(define (f g) (g 2))
then we can
(define (square x) (* x x)) (f square)
4
and
(f (lambda (z) (* z (+ z 1))))
6
but what if we (f f)?
(f f)
scheme says:
ERROR: Wrong type to apply: 2 0 (_ 2)
… i dunno! applying 2 to 2?