Add solutions to exercises from subsetion 2.2.2

This commit is contained in:
Petar Kapriš 2025-02-05 18:13:20 +01:00
parent da96368e0b
commit 8f4c2125bc
9 changed files with 146 additions and 0 deletions

20
ex-2.24.txt Normal file
View file

@ -0,0 +1,20 @@
Result:
(1 (2 (3 4)))
Box-and-pointer diagram:
[.|.] -> [.|.] -> nil
V V
1 [.|.] -> [.|.] -> nil
V V
2 [.|.] -> [.|.] -> nil
V V
3 4
Tree structure:
(1 (2 (3 4)))
/ \
1 (2 (3 4))
/ \
2 (3 4)
/ \
3 4

3
ex-2.25.txt Normal file
View file

@ -0,0 +1,3 @@
cadaddr
caar
cadadadadadadr

3
ex-2.26.txt Normal file
View file

@ -0,0 +1,3 @@
(1 2 3 4 5 6)
((1 2 3) 4 5 6)
((1 2 3) (4 5 6))

15
ex-2.27.scm Normal file
View file

@ -0,0 +1,15 @@
#lang sicp
(define (dr-aux x acc)
(if (null? x)
acc
(let ((head (car x))
(tail (cdr x)))
(if (pair? head)
(dr-aux tail
(cons (deep-reverse head)
acc))
(dr-aux tail
(cons head acc))))))
(define (deep-reverse x)
(dr-aux x '()))

16
ex-2.28.scm Normal file
View file

@ -0,0 +1,16 @@
#lang sicp
(define (list? x)
(or (null? x) (pair? x)))
(define (fringe tree)
(if (null? tree)
'()
(let* ((head (car tree))
(tail (cdr tree))
(h-list (if (list? head) ; if it's a subtree
(fringe head) ; turn it into a list
; otherwise, just make a
; one-element list
(list head))))
(append h-list (fringe tail)))))

60
ex-2.29.scm Normal file
View file

@ -0,0 +1,60 @@
#lang sicp
(define (make-mobile left right)
(list left right))
; a. part 1
(define (left-branch mobile)
(car mobile))
(define (right-branch mobile)
(cadr mobile))
(define (make-branch length structure)
(list length structure))
; a. part 2
(define (branch-length branch)
(car branch))
(define (branch-structure branch)
(cadr branch))
; b
(define (total-weight mobile)
(if (number? mobile)
mobile
(+ (total-weight (branch-structure (left-branch mobile)))
(total-weight (branch-structure (right-branch mobile))))))
; c, with O(n) complexity, because the basic algorithm would
; recalculate weights of lower level submobiles, each node is
; calculated h times, where h is the number of it's ancestors
; for a typical mobile, this would cause the complexity to be
; O(log(n) * n)
(define (mobile-balanced? mobile)
; this returns a pair, a bool describing whether the mobile
; is balanced, and a number, describing the total weight of
; the mobile
(define (balanced-aux mobile)
(if (number? mobile)
(cons #t mobile)
(let* ((left-submobile (branch-structure (left-branch mobile)))
(right-submobile (branch-structure (right-branch mobile)))
(len-left (branch-length (left-branch mobile)))
(len-right (branch-length (right-branch mobile)))
(bal-aux-left (balanced-aux left-submobile))
(bal-aux-right (balanced-aux right-submobile))
(balanced-left? (car bal-aux-left))
(balanced-right? (car bal-aux-right))
(weight-left (cdr bal-aux-left))
(weight-right (cdr bal-aux-right)))
(cons (and balanced-left?
balanced-right?
(= (* weight-left len-left)
(* weight-right len-right)))
(+ weight-left weight-right)))))
(car (balanced-aux mobile)))
; d. The way we wrote the code so far, we've managed to
; cleanly separate the internals of the mobile and branch
; structures from the code which uses them, so the only thing
; that needs to be changed is the selectors. In this case in
; particular, we only need to change the right-branch, and
; branch-structure selectors, by changing the cadr into a cdr

13
ex-2.30.scm Normal file
View file

@ -0,0 +1,13 @@
#lang sicp
(define (square x) (* x x))
;(define (square-tree tree)
; (cond ((null? tree) '())
; ((number? tree) (square tree))
; (else (cons (square-tree (car tree))
; (square-tree (cdr tree))))))
(define (square-tree tree)
(if (number? tree)
(square tree)
(map square-tree tree)))

7
ex-2.31.scm Normal file
View file

@ -0,0 +1,7 @@
#lang sicp
(define (tree-map f tree)
(cond ((null? tree) '())
((pair? tree) (cons (tree-map f (car tree))
(tree-map f (cdr tree))))
(else (f tree))))

9
ex-2.32.scm Normal file
View file

@ -0,0 +1,9 @@
#lang sicp
(define (subsets s)
(if (null? s)
(list nil)
(let ((rest (subsets (cdr s))))
(append rest (map (lambda (set) (cons (car s)
set))
rest)))))