## Solution to SICP Exercise 2.2

Solution to Exercise 2.2:

```(define (make-segment start end) (cons start end))```

``` (define (start-segment s) (car s)) (define (end-segment s) (cdr s)) (define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (avg a b) (/ (+ a b) 2)) ```

```(define (midpoint-segment s) (let ((start (start-segment s)) (end (end-segment s))) (make-point (avg (x-point start) (x-point end)) (avg (y-point start) (y-point end))))) ```

## Solution to SICP Exercise 2.1

Solution to Exercise 2.1:

```(define (make-rat n d) (let ((new-n (if (negative? d) (- n) n)) (new-d (abs d))) (let ((g (gcd new-n new-d))) (cons (/ new-n g) (/ new-d g))))) ```

## Solution to SICP Exercise 1.46

Solution to Exercise 1.46:

```(define (iterative-improve good-enough? improve) (define (iter initial-guess) (if (good-enough? initial-guess) initial-guess (iter (improve initial-guess)))) iter) ```

## Solution to SICP Exercise 1.44

Solution to Exercise 1.44:

```(define (average-3 a b c) (/ (+ a b c) 3))```

``` (define (smooth f dx) (lambda (x) (average-3 (f (- x dx)) (f x) (f (+ x dx))))) ```

```(define (n-fold-smooth f dx n) (repeated (lambda (g) (smooth g dx)) n) f) ```

## Solution to SICP Exercise 1.43

Solution to Exercise 1.43:

```(define (compose f g) (lambda (x) (f (g x)))) (define (inc i) (+ i 1))```

``` ```

```(define (repeated f n) (define (iter i result) (if (= i n) result (iter (inc i) (compose f result)))) (iter 1 f)) ```

## Solution to SICP Exercise 1.41

Solution to Exercise 1.41:

```(define (double f) (lambda (x) (f (f x)))) ```

Output:

```> (define (inc x) (+ x 1)) > (((double (double double)) inc) 5) 21 ```

## Solution to SICP Exercise 1.39

Solution to Exercise 1.39:

```(define (cont-frac n d k) (define (iter i sub-expr) (if (= i 0) sub-expr (iter (- i 1) (/ (n i) (+ (d i) sub-expr))))) (iter (- k 1) (/ (n k) (d k))))```

``` ```

```(define (tan-cf x k) (let ((x-squared-negated (- (* x x)))) (cont-frac (lambda (i) (if (= i 1) x x-squared-negated)) (lambda (i) (- (* 2 i) 1)) k))) ```

## Solution to SICP Exercise 1.38

Solution to Exercise 1.38:

```(define (cont-frac n d k) (define (iter i sub-expr) (if (= i 0) sub-expr (iter (- i 1) (/ (n i) (+ (d i) sub-expr))))) (iter (- k 1) (/ (n k) (d k))))```

``` ```

```(define (e k) (+ (cont-frac (lambda (i) 1.0) (lambda (i) (if (= (remainder (+ i 1) 3) 0) (* 2 (/ (+ i 1) 3)) 1.0)) k) 2)) ```

## Solution to SICP Exercise 1.37

Solution to Exercise 1.37:

```; part a (recursive process) (define (cont-frac n d k) (define (sub-expr i) (if (= i k) (/ (n i) (d i)) (/ (n i) (+ (d i) (sub-expr (+ i 1)))))) (sub-expr 1))```

``` (define (golden-ratio k) (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) k)) (define (iterations-required) (define (iter i previous) (let ((current (golden-ratio i))) (if (= (round (* current 10000)) (round (* previous 10000))) (- i 1) (iter (+ i 1) current)))) (iter 1 0.0)) ; > (iterations-required) ; 12 ```

```; part b (iterative process) (define (cont-frac-i n d k) (define (iter i sub-expr) (if (= i 0) sub-expr (iter (- i 1) (/ (n i) (+ (d i) sub-expr))))) (iter (- k 1) (/ (n k) (d k)))) ```