learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

commit f762fbacedb2279d361f69354c6661df154741c4
parent c5567c05930e1c11f66aac2b2f8b163126fa127c
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Sun, 12 Mar 2023 15:19:10 +0200

Add exercises 2.38 and 2.39 solutions.

Diffstat:
Mguile.scm | 198+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 198 insertions(+), 0 deletions(-)

diff --git a/guile.scm b/guile.scm @@ -2517,3 +2517,201 @@ Iterative. (test-end "2.37")) (exercise-2.37) + +;; Exercise 2.38 + +(define (my-fold-right op initial sequence) + (accumulate op initial sequence)) + +(define (my-fold-left op initial sequence) + (define (iter result rest) + (if (null? rest) + result + (iter (op result (car rest)) + (cdr rest)))) + (iter initial sequence)) + +(define (exercise-2.38) + (define my-fold-right-/-ratsui + (/ 1 + (/ 2 + (/ 3 + 1)))) + (define my-fold-left-/-ratsui + (/ (/ (/ 1 + 1) + 2) + 3)) + (define my-fold-right-list-ratsui + (list 1 + (list 2 + (list 3 + '())))) + (define my-fold-left-list-ratsui + (list (list (list '() + 1) + 2) + 3)) + + (test-begin "2.38") + (test-equal my-fold-right-/-ratsui + (my-fold-right / + 1 + (list 1 2 3))) + (test-equal my-fold-right-/-ratsui + (my-fold-right / + (/ 3 + 1) + (list 1 2))) + (test-equal my-fold-right-/-ratsui + (my-fold-right / + (/ 2 + (/ 3 + 1)) + (list 1))) + (test-equal my-fold-right-/-ratsui + (my-fold-right / + (/ 1 + (/ 2 + (/ 3 + 1))) + (list))) + (test-equal my-fold-left-/-ratsui + (my-fold-left / + 1 + (list 1 2 3))) + (test-equal my-fold-left-/-ratsui + (my-fold-left / + (/ 1 + 1) + (list 2 3))) + (test-equal my-fold-left-/-ratsui + (my-fold-left / (/ (/ 1 + 1) + 2) + (list 3))) + (test-equal my-fold-left-/-ratsui + (my-fold-left / (/ (/ (/ 1 + 1) + 2) + 3) + (list))) + (test-equal my-fold-right-list-ratsui + (my-fold-right list + '() + (list 1 2 3))) + (test-equal my-fold-right-list-ratsui + (my-fold-right list + (list 3 '()) + (list 1 2))) + (test-equal my-fold-right-list-ratsui + (my-fold-right list + (list 2 (list 3 '())) + (list 1))) + (test-equal my-fold-right-list-ratsui + (my-fold-right list + (list 1 (list 2 (list 3 '()))) + (list))) + (test-equal my-fold-left-list-ratsui + (my-fold-left list + '() + (list 1 2 3))) + (test-equal my-fold-left-list-ratsui + (my-fold-left list + (list '() 1) + (list 2 3))) + (test-equal my-fold-left-list-ratsui + (my-fold-left list + (list (list '() 1) 2) + (list 3))) + (test-equal my-fold-left-list-ratsui + (my-fold-left list + (list (list (list '() 1) 2) 3) + (list))) + (test-end "2.38") + + ;; if op: A -> A, + ;; (cut my-fold-right op <> <>) is equivalent to (cut my-fold-left op <> <>) + ;; iff + ;; for all a, b, c in A, + ;; (op (op a b) c) = (op a (op b c)). + ;; Did I get that right? Seems right. + ) + +(exercise-2.38) + +;; Exercise 2.39 + +(define (exercise-2.39) + (define (reverse-my-fold-right sequence) + (my-fold-right + (lambda (x y) + (append y + (list x))) + '() + sequence)) + + (define (reverse-my-fold-left sequence) + (my-fold-left + (lambda (x y) + (append (list y) + x)) + '() + sequence)) + + (test-begin "2.39") + (test-equal + '(4 3 2 1) + (reverse-my-fold-right '(1 2 3 4))) + (test-equal + '(4 3 2 1) + (reverse-my-fold-left '(1 2 3 4))) + (test-end "2.39")) + +(exercise-2.39) + +;; Exercise 2.40 + +(define (exercise-2.40) + (define (f n) + (accumulate + append + '() + (map (lambda (i) + (map (lambda (j) + (list i j)) + (enumerate 1 (1- i)))) + (enumerate-interval 1 n)))) + + (define (flatmap proc seq) + (accumulate append + '() + (map proc + seq))) + + (define (g n) + (flatmap (lambda (x) x) (enumerate-interval 1 n))) + + (define (prime-sum? pair) + (prime? (+ (car pair) + (cadr pair)))) + + (define (make-pair-sum pair) + (list (car pair) + (cadr pair) + (+ (car pair) + (cadr pair)))) + + (define (prime-sum-pairs n) + (map make-pair-sum + (filter prime-sum? + (flatmap (lambda (i) + (list i j)) + (enumerate-interval 1 + (1- i)))))) + + + (test-begin "2.40") + (test-end "2.40")) + +(exercise-2.40)