Zapiski #3 srečanja programerskega bralnega krožka SICP
-
-
Teme
-
+
+
-
Teme
+
-
-Funkcije višjega reda
+
+
Funkcije višjega reda
-
Vaje
-
+
+
-
Vaje
+
-
1.29 Simpsonovo pravilo
-
+
+
-
1.29 Simpsonovo pravilo
+
-
(define(cube x) +(define(cube x) (* x x x)) -(define(sum-integers a b) - (if ( > a b) +(define(sum-integers a b) + (if ( > a b) 0 (+ a (sum-integers (+ a 1) b)))) -(define(sum-cubes a b) - (if (> a 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) +(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)) +(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)) +(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)) @@ -273,23 +273,23 @@
-
1.30 linearen sum
-
+
+
1.30 linearen sum
+Rekurzivne procedure:
-
-(define (sum term a next b) - (if (> a b) +(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 (cube n) (* n n n)) +(define (inc n) (+ n 1)) -(define (sum-cubes a b) +(define (sum-cubes a b) (sum cube a inc b)) (sum-cubes 1 10) @@ -305,17 +305,17 @@ Rekurzivne procedure: Iterativne procedure:-(define (sum-iter term a next b) - (define (iter a result) - (if (> a b) +(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 (cube n) (* n n n)) +(define (inc n) (+ n 1)) -(define (sum-cubes-iter a b) +(define (sum-cubes-iter a b) (sum-iter cube a inc b)) (sum-cubes-iter 1 10) @@ -328,39 +328,217 @@ Iterativne procedure:
-
+1.31 Produkt višjega reda
-
+
+
+
+
+
+1.31 Produkt višjega reda
+
-
+
+(define (prod-iter term a next b) - (define (iter a result) - (if (> a b) +(define (prod-iter term a next b) + (define (iter a result) + (if (> a b) result (iter (next a) (* result (term a))))) - (iter a 0)) + (iter a 1)) -(define (piblizek n) - (define (stevec n) - (+ n 2)) - (* 4 (/ (prod-iter (lambda (n) (* (- n 1) (+ n 1))) +(define (piblizek n) + (define (stevec m) + (+ m 2)) + (* 4 (/ (prod-iter (lambda (a) (* (- a 1) (+ a 1))) 3 stevec n) - (prod-iter (lambda (n) (* n n)) + (prod-iter (lambda (b) (* b b)) 3 stevec n)))) -(piblizek 10) +(+ (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! +TODO popravi!! ^
+
+Created: 2024-06-12 sre 19:35
+ +