Zapiski #3 srečanja programerskega bralnega krožka SICP
Teme
Funkcije višjega reda
Vaje
1.29 Simpsonovo pravilo
(define(cube x) (* x x x)) (define(sum-integers a b) (if ( > a b) 0 (+ a (sum-integers (+ a 1) b)))) (define(sum-cubes a b) (if (> a b) 0 (+ (cube a) (sum-cubes(+ a 1) b)))) (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.0)) add-dx b) dx)) (define (simpson f a b n) (define h (/ (- b a) n)) (define (add-hh a) (+ a h h)) (* (+ (- (f a)) (* 2 (sum f a add-hh b)) (* 4 (sum f (+ a h) add-hh b)) (- (f b))) (/ h 3.0))) (list (simpson cube 1 2 100) (simpson cube 1 2 1000))
3.7500000000000004 | 3.75 |
1.30 linearen sum
Rekurzivne procedure:
(define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (cube n) (* n n n)) (define (inc n) (+ n 1)) (define (sum-cubes a b) (sum cube a inc b)) (sum-cubes 1 10)
3025
Iterativne procedure:
(define (sum-iter term a next b) (define (iter a result) (if (> a b) result (iter (next a) (+ result (term a))))) (iter a 0)) (define (cube n) (* n n n)) (define (inc n) (+ n 1)) (define (sum-cubes-iter a b) (sum-iter cube a inc b)) (sum-cubes-iter 1 10)
3025
1.31 Produkt višjega reda
(define (prod-iter term a next b) (define (iter a result) (if (> a b) result (iter (next a) (* result (term a))))) (iter a 1)) (define (piblizek n) (define (stevec m) (+ m 2)) (* 4 (/ (prod-iter (lambda (a) (* (- a 1) (+ a 1))) 3 stevec n) (prod-iter (lambda (b) (* b b)) 3 stevec n)))) (+ (piblizek 10000) 0.0)
3.1417497371492673
1.32 Akumulator (reduce funkcija)
(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 (sum term a next b) (accumulate + 0 term a next b)) (define (cube n) (* n n n)) (define (sum-cubes a b) (sum cube a (lambda (n) (+ n 1)) b)) (define (prod term a next b) (accumulate * 1 term a next b)) (define (piblizek n) (define (stevec m) (+ m 2)) (* 4 (/ (prod (lambda (a) (* (- a 1) (+ a 1))) 3 stevec n) (prod (lambda (b) (* b b)) 3 stevec n)))) (list (sum-cubes 1 10) (+ (piblizek 10000) 0.0))
3025 | 3.1417497371492673 |
1.32b Akumulator iterativno (reduce funkcija)
(define (accumulate-iter combiner null-value term a next b) (define (combine a combination) (if (> a b) combination (combine (next a) (combiner combination (term a))))) (combine a null-value)) (define (sum term a next b) (accumulate-iter + 0 term a next b)) (define (cube n) (* n n n)) (define (sum-cubes a b) (sum cube a (lambda (n) (+ n 1)) b)) (define (prod term a next b) (accumulate-iter * 1 term a next b)) (define (piblizek n) (define (stevec m) (+ m 2)) (* 4 (/ (prod (lambda (a) (* (- a 1) (+ a 1))) 3 stevec n) (prod (lambda (b) (* b b)) 3 stevec n)))) (list (sum-cubes 1 10) (+ (piblizek 10000) 0.0))
3025 | 3.1417497371492673 |
1.33 filtriran akumulator
a) vsota kvadratov praštevil med a in b
(define (accumulate-filter combiner null-value term a next b predikat?) (if (> a b) null-value (if (predikat? a) (combiner (term a) (accumulate-filter combiner null-value term (next a) next b predikat?)) (accumulate-filter combiner null-value term (next a) next b predikat?)))) (define (je-prastevilo? a) (define (preveri-prastevilo? x a) (if (> x (/ a 2)) #t (and (not (integer? (/ a x))) (preveri-prastevilo? (+ x 1) a)))) (preveri-prastevilo? 2 a)) ;; (vsota kvadratov praštevil med 1 in 12 je 209) (= (accumulate-filter + 0 (lambda (n) (* n n)) 1 (lambda (n) (+ n 1)) 12 je-prastevilo?) 209)
a) produkt vseh naravnih števil manjših od n ki so "relativno pra-n" (torej si z n ne delijo skupnega delitelja razen n)
(define (accumulate-filter combiner null-value term a next b predikat?) (if (> a b) null-value (if (predikat? a) (combiner (term a) (accumulate-filter combiner null-value term (next a) next b predikat?)) (accumulate-filter combiner null-value term (next a) next b predikat?)))) (define (id a) a) (define (inc a) (+ a 1)) (define (IN a b) (and a b)) (define (relativni-prastevili? a b) (accumulate-filter IN #t (lambda (x) (and (not (integer? (/ a x))) (not (integer? (/ b x))))) 2 inc a id)) (define (produkt-relativnih-prastevil n) (accumulate-filter * 1 id 1 inc n (lambda (x) (relativni-prastevili? x n)))) (produkt-relativnih-prastevil 9)
TODO popravi!! ^