* 1.30 #+BEGIN_SRC scheme (define (sum term a next b) (define (iter term a next b result) (if (> a b) (iter ( (iter term a next b 0)) #+END_SRC * 1.31 #+BEGIN_SRC scheme (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)) #+END_SRC * 1.32 #+BEGIN_SRC scheme (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) #+END_SRC #+RESULTS: : 0.33433325000000047 * 1.33 #+BEGIN_SRC scheme (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)) #+END_SRC * 1.34 suppose we define #+BEGIN_SRC scheme :noweb yes :session s (define (f g) (g 2)) #+END_SRC then we can #+BEGIN_SRC scheme :session s (define (square x) (* x x)) (f square) #+END_SRC #+RESULTS: : 4 and #+BEGIN_SRC scheme :session s (f (lambda (z) (* z (+ z 1)))) #+END_SRC #+RESULTS: : 6 but what if we (f f)? #+BEGIN_SRC scheme :session s (f f) #+END_SRC scheme says: : ERROR: Wrong type to apply: 2 : : 0 (_ 2) ... i dunno! applying 2 to 2?