closed
close, closure, closed
closed
close, closure, closed
Exercise 2.76.
Exercise 2.75.
(define (make-from-mag-ang r a)
(define (dispatch op)
(cond ((eq? op 'real-part)
(* r (cos a)))
((eq? op 'imag-part)
(* r (sin a)))
((eq? op 'magnitude) r)
((eq? op 'angle) a)
(else
(error "Unknown op -- MAKE-FROM-MAG-ANG" op))))
dispatch)
Exercise 2.73.
a.
Because it cannot predicate only by the operator of expression for number? and variable? cases.
b. Install for derivatives of sums
(define (install-deriv-sum-package)
(define (deriv-sum addends var)
(cons '+
(map (lambda (addend) (deriv addend var))
addends)))
(put 'deriv '+ deriv-sum))
Install for derivatives of products using message passing
(define (install-deriv-product-package)
(define (deriv-product multipliers var)
(define (make-items appending rest result)
(if (null? rest) result
(make-items
(lambda (appendand) (appending (cons (car rest) appendand)))
(cdr rest)
(cons (cons
'*
(appending (cons (deriv (car rest) var) (cdr rest))))
result))))
(cons '+ (make-items identity
multipliers
nil)))
(put 'deriv '* deriv-product))
c. install for derivatives of exponents
(define (install-deriv-exponent-package)
(define (deriv-exponent exponentiation var)
(let ((u (car exponentiation))
(n (cadr exponentiation)))
(list '*
n
(list '** u (list '- n 1))
(list u var))))
(put 'deriv '** deriv-exponent))
d. We need to change the installation by change registering in the opposite way like:
(put '+ 'deriv deriv-plus)
Exercise 2.32.
(define (subsets s)
(if (null? s)
(list nil)
(let ((rest (subsets (cdr s))))
(append rest
(map (lambda (subset)
(cons (car s)
subset))
rest)))))
Exercise 2.31.
(define (tree-map proc tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map proc sub-tree)
(proc sub-tree)))
tree))
Exercise 2.30.
directly defined
(define (square-tree tree)
(cond ((null? tree) null)
((pair? tree)
(cons (square-tree (car tree))
(square-tree (cdr tree))))
(else (square tree))))
using map and recursion defined
(define (square-tree-map tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree-map sub-tree)
(square sub-tree)))
tree))
Exercise 2.29.
a.
(define left-branch car)
(define right-branch cadr)
(define branch-length car)
(define branch-structure cadr)
b. first, we introduced
(define (left-branch-structure mobile)
(branch-structure (left-branch mobile)))
(define (right-branch-structure mobile)
(branch-structure (right-branch mobile)))
then we can calculate total-weight
(define (total-weight mobile)
(if (pair? mobile)
(+ (total-weight (left-branch-structure mobile))
(total-weight (right-branch-structure mobile)))
mobile))
c.
(define (balanced? mobile)
(if (pair? mobile)
(and (= (branch-torque (left-branch mobile))
(branch-torque (right-branch mobile)))
(balanced? (left-branch-structure mobile))
(balanced? (right-branch-structure mobile)))
#t))
d. we just need to change
(define right-branch cdr)
(define branch-structure cdr)
Exercise 2.28.
(define (append list1 list2)
(if (null? list1)
list2
(cons (car list1) (append (cdr list1) list2))))
(define (fringe items)
(cond
((null? items) null)
((pair? items) (append (fringe (car items)) (fringe (cdr items))))
(else (list items))))
Exercise 2.27.
(define (deep-reverse items)
(define (iter rest result)
(cond
((null? rest) result)
(else
(iter (cdr rest) (cons (deep-reverse (car rest)) result)))))
(if (pair? items)
(iter items null)
items))
Exercise 2.26.
> (append x y)
'(1 2 3 4 5 6)
> (cons x y)
'((1 2 3) 4 5 6)
> (list x y)
'((1 2 3) (4 5 6))
Exercise 2.25.
(car (cdaddr (list 1 3 (list 5 7) 9)))
(car (car (list (list 7))))
(cadadr (cadadr (cadadr '(1 (2 (3 (4 (5 (6 7)))))))))
(cons (list 1 2) (list 3 4))
(cons (list 1 2) (list 3 4))
(cons (list 1 2) (cons 3 (cons 4 null)))
(list (list 1 2) 3 4)
Exercise 2.23.
(define (for-each proc items)
(cond ((null? items) #t)
(else
(proc (car items))
(for-each proc (cdr items)))))
Exercise 2.22.
We can use substitution model to explain this:
(square-list (list 1 2 3))
(iter (list 1 2 3) null)
(iter (list 2 3) (cons (square 1) null))
(iter (list 3) (cons (square 2) (cons (square 1) null)))
(iter '() (cons (square 3) (cons (square 2) (cons (square 1)))))
(cons (square 3) (cons (square 2) (cons (square 1))))
'(9 4 1)
Exercise 2.21.
(define (square x)
(* x x))
(define (map proc items)
(if (null? items)
null
(cons (proc (car items))
(map proc (cdr items)))))
(define (square-list1 items)
(if (null? items)
null
(cons
(square (car items))
(square-list (cdr items)))))
(define (square-list items)
(map square items))
Exercise 2.20.
(define (same-parity . items)
(define (same-oddity? a b)
(= 0 (remainder (+ a b) 2)))
(define first-item (car items))
(define (recur _items)
(if (null? _items)
null
(if (same-oddity? first-item (car _items))
(cons
(car _items)
(recur (cdr _items)))
(recur (cdr _items)))))
(recur items))
Exercise 2.19.
(define no-more? null?)
(define except-first-denomination cdr)
(define first-denomination car)
Exercise 2.18.
The Iterative version is:
(define (reverse items)
(define (iter rest result)
(if (null? rest)
result
(iter (cdr rest) (cons (car rest) result))))
(iter items null))
Exercise 2.17.
(define (last-pair items)
(if (null? (cdr items))
(car items)
(last-pair (cdr items))))
Exercise 2.54.
(define (equal? a b)
(if (and (pair? a) (pair? b))
(and
(equal? (car a) (car b))
(equal? (cdr a) (cdr b)))
(eq? a b)))
Exercise 2.55.
(car ''abracadabra)
(car (quote (quote abracadabra)))
(quote quote)
'quote
Exercise 2.53.
> (cdr '((x1 x2) (y1 y2)))
'((y1 y2))
> (list (list 'george))
'((george))
> (list 'a 'b 'c)
'(a b c)
> (cadr '((x1 x2) (y1 y2)))
'(y1 y2)
> (pair? (car '(a short list)))
#f
> (memq 'red '((red shoes) (blue socks)))
#f
> (memq 'red '(red shoes blue socks))
'(red shoes blue socks)
It is not immediately apparent that the script depends on an external library. If a dependency is missing, or included in the wrong order, the application will not function properly. If a dependency is included but not used, the browser will be forced to download unnecessary code.
Figure 2.1
(if (< d 0) (cons (- n) (- d)) (cons n d))
Exercise 2.6.
one is
one
;
(add-1 zero)
;
(lambda (f)
(lambda (x)
(f (((lambda (f) (lambda (x) x)) f) x))))
;
(lambda (f)
(lambda (x)
(f ((lambda (x) x) x))))
;
(lambda (f)
(lambda (x)
(f x)))
two is
two
;
(add-1 one)
;
(lambda (f)
(lambda (x)
(f ((one f) x))))
;
(lambda (f)
(lambda (x)
(f ((lambda (x) (f x)) x))))
;
(lambda (f)
(lambda (x)
(f (f x))))
and add is
(define (add n m)
(define (compose f g)
(lambda (x) (f (g x))))
(lambda (f)
(compose (n f) (m f))))
also we can evaluate it to:
(define (add n m)
(lambda (f)
(lambda (x)
((n f) ((m f) x)))))
Exercise 2.5.
(define (cons x y)
(* (expt 2 x) (expt 3 y)))
(define (car z)
(define (iter n result)
(if (= 0 (remainder n 2)) (iter (/ n 2) (+ result 1)) result))
(iter z 0))
(define (cdr z)
(define (iter n result)
(if (= 0 (remainder n 3)) (iter (/ n 3) (+ result 1)) result))
(iter z 0))
Exercise 2.3.
We can make another layer of data barrier for selecting vertical-side and horizontal-side of rectangle.
(define (make-rectangle x1 y1 x2 y2)
(make-segment x1 y1 x2 y2))
(define (horizontal-side rectangle)
(abs (-
(x-point (start-segment rectangle))
(x-point (end-segment rectangle)))))
(define (vertical-side rectangle)
(abs (-
(y-point (start-segment rectangle))
(y-point (end-segment rectangle)))))
(define (perimeter-of-rectangle rectangle)
(* 2 (+
(horizontal-side rectangle)
(vertical-side rectangle))))
(define (area-of-rectangle rectangle)
(*
(horizontal-side rectangle)
(vertical-side rectangle)))
Exercise 2.2.
(define (make-point x y)
(cons x y))
(define (x-point point)
(car point))
(define (y-point point)
(cdr point))
(define (make-segment x1 y1 x2 y2)
(cons (make-point x1 y1) (make-point x2 y2)))
(define (start-segment segment)
(car segment))
(define (end-segment segment)
(cdr segment))
(define (midpoint-segment segment)
(define (average a b) (/ (+ a b) 2))
(make-point
(average
(x-point (start-segment segment))
(x-point (end-segment segment)))
(average
(y-point (start-segment segment))
(y-point (end-segment segment)))))
Exercise 1.43.
(define (repeated f n)
(if (= n 0)
identity
(lambda (x) ((repeated f (- n 1)) (f x)))))
Exercise 1.42.
(define (compose f g)
(lambda (x) (f (g x))))
Exercise 1.41.
(define (double f)
(lambda (x) (f (f x))))
Exercise 1.40.
(define (cubic a b c)
(lambda (x) (+ (* x x x) (* a x x) (* b x) c)))
Exercise 1.38.
(define (e k)
(define (n i) 1)
(define (d i)
(if (= (remainder i 3) 2) (* (+ (quotient i 3) 1) 2) 1))
(+ (cont-frac n d k) 2))
Exercise 1.37.
(define (cont-frac n d k)
(define (iter i result)
(if (= i 0)
result
(iter (- i 1) (/ (n i) (+ (d i) result)))))
(iter k 0))
k should be 11 in order to get an approximation that is accurate to 4 decimal places.
> (cont-frac (lambda (i) 1.0)
(lambda (i) 1.0)
10)
0.6179775280898876
> (cont-frac (lambda (i) 1.0)
(lambda (i) 1.0)
11)
0.6180555555555556
The recursive one is
(define (cont-frac-recursive n d k)
(define (recur i)
(if (> i k)
0
(/ (n i) (+ (d i) (recur (+ i 1))))))
(recur 1))
Exercise 1.36.
> (fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 5)
4.64601483711009
4.571611286076025
4.558294317536066
4.556006022881116
4.555615799731297
4.555549342575593
4.555538027101999
4.5555361005218895
4.5555361005218895
> (fixed-point (lambda (x) (/ (log 1000) (log x))) 5)
4.29202967422018
4.741863119908242
4.438204569837609
4.635299887107611
4.50397811613643
4.589989462723705
4.53301150767844
4.570475672855484
4.545720389670642
4.562024936588171
4.551263234080531
4.55835638768598
4.553676852183342
4.55676216434628
4.554727130670954
4.556069054770006
4.555184018843625
4.5557676565438205
4.555382746639082
4.55563658243586
4.555469180245326
4.555579577900997
4.5555067722873686
4.5555547860484085
4.555523121789556
4.555544003742869
4.555530232469306
4.555539314360711
4.555539314360711
that is 8 vs 28
Exercise 1.35.
(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)
Exercise 1.34.
It will evaluated to (2 2)
Exercise 1.33.
(define (filtered-accumulator filter combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter
(next a)
(combiner
result
(if (filter a)
(term a)
null-value)))))
(iter a null-value))
(define (sum-prime a b)
(filtered-accumulator
prime? + 0 square a add1 b))
(define (product-n-coprime n)
(define (n-coprime x)
(= 1 (gcd x n)))
(filtered-accumulator
n-coprime? * 1 i 1 add1 n))
Exercise 1.32.
(define (accumulate combiner null-value term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (combiner result (term a)))))
(iter a null-value))
(define (new-sum term a next b)
(accumulate + 0 term a next b))
(define (new-product term a next b)
(accumulate * 1 term a next b))
Exercise 1.31.
product iterative process is
(define (product term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* result (term a)))))
(iter a 1))
Calculating \(\pi\) with times
(define (pi times)
(* 4.0
(/
(* (product i 2 add2 (* 2 times)) (product i 4 add2 (+ (* 2 times) 2)))
(square (product i 3 add2 (+ (* 2 times) 1))))))
Exercise 1.30.
(define (sum term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (+ result (term a)))))
(iter a 0))
Exercise 1.20.
In the normal-order evaluation remainder will be
performed operations for 1+2+4+7+4=18 times .
(gcd 206 40)
(if (= 40 0) 206 (gcd 40 (r 206 40)))
(gcd 40 (r 206 40))
(if (= (r 206 40) 0) 40 (gcd (r 206 40) (r 40 (r 206 40)))) ;x1
(gcd (r 206 40) (r 40 (r 206 40)))
;if... x2, last is (r 40 6)
(gcd (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40))))
;if... x4, last is (r 6 4)
(gcd (r (r 206 40) (r 40 (r 206 40))) (r (r 40 (r 206 40)) (r (r 206 40) (r 40 (r 206 40)))))
;if... x7, last is (r 4 2) which is 0
(r (r 206 40) (r 40 (r 206 40))) ;4x
2
In the applicative-order evaluation remainder will be
performed operations for 4 times .
(gcd 206 40)
(if (= 40 0) 206 (gcd 40 (r 206 40)))
(gcd 40 (r 206 40)) ;x1
(gcd 40 6)
(if (= 6 0) 40 (gcd 6 (r 40 6))) ;x1
(gcd 6 4)
(if (= 4 0) 6 (gcd 4 (r 6 4))) ;x1
(gcd 4 2)
(if (= 2 0) 4 (gcd 2 (r 4 2))); x1
(gcd 2 0)
(if (= 0 0) 2 (gcd 0 (r 2 0)));
2
Exercise 1.23.
Change find-divisor as below,
(define (find-divisor n test-divisor)
(define (next divisor)
(if (= 2 divisor) 3 (+ divisor 2)))
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (next test-divisor)))))
<table>
<tr>
<th>prime</th>
<th>duration</th>
<th>modified duration</th>
<th>ratio</th>
</tr>
<tr>
<td>1009</td>
<td>0.001953125</td>
<td>0.001953125</td>
<td>1</td>
</tr>
<tr>
<td>1013</td>
<td>0.003173828125</td>
<td>0.001953125</td>
<td>1.625</td>
</tr>
<tr>
<td>1019</td>
<td>0.0029296875</td>
<td>0.001953125</td>
<td>1.5</td>
</tr>
<tr>
<td>10007</td>
<td>0.008056640625</td>
<td>0.005126953125</td>
<td>1.5714285714285714</td>
</tr>
<tr>
<td>10009</td>
<td>0.007080078125</td>
<td>0.005126953125</td>
<td>1.380952380952381</td>
</tr>
<tr>
<td>10037</td>
<td>0.008056640625</td>
<td>0.005126953125</td>
<td>1.5714285714285714</td>
</tr>
<tr>
<td>100003</td>
<td>0.02294921875</td>
<td>0.014892578125</td>
<td>1.540983606557377</td>
</tr>
<tr>
<td>100019</td>
<td>0.02197265625</td>
<td>0.01513671875</td>
<td>1.4516129032258065</td>
</tr>
<tr>
<td>100043</td>
<td>0.02197265625</td>
<td>0.013916015625</td>
<td>1.5789473684210527</td>
</tr>
<tr>
<td>1000003</td>
<td>0.06689453125</td>
<td>0.0439453125</td>
<td>1.5222222222222221</td>
</tr>
<tr>
<td>1000033</td>
<td>0.06689453125</td>
<td>0.044189453125</td>
<td>1.5138121546961325</td>
</tr>
<tr>
<td>1000037</td>
<td>0.06689453125</td>
<td>0.05615234375</td>
<td>1.191304347826087</td>
</tr>
</table>
Exercise 1.22.
(define (search-for-primes start)
(define (search-for-primes-iter odd total)
(if (= total 3) (display " done ")
(search-for-primes-iter
(next-odd odd)
(+ total (if (timed-prime-test odd) 1 0)))))
(define (next-odd number)
(+ number (if (even? number) 1 2)))
(search-for-primes-iter (next-odd start) 0))
The three smallest primes larger than 1,000
> (search-for-primes 1000)
1001
1003
1005
1007
1009 *** 0.001953125
1011
1013 *** 0.003173828125
1015
1017
1019 *** 0.0029296875 done
The three smallest primes larger than 10,000
> (search-for-primes 10000)
10001
10003
10005
10007 *** 0.008056640625
10009 *** 0.007080078125
10011
10013
10015
10017
10019
10021
10023
10025
10027
10029
10031
10033
10035
10037 *** 0.008056640625 done
The three smallest primes larger than 100,000
> (search-for-primes 100000)
100001
100003 *** 0.02294921875
100005
100007
100009
100011
100013
100015
100017
100019 *** 0.02197265625
100021
100023
100025
100027
100029
100031
100033
100035
100037
100039
100041
100043 *** 0.02197265625 done
The three smallest primes larger than 1,000,000
> (search-for-primes 1000000)
1000001
1000003 *** 0.06689453125
1000005
1000007
1000009
1000011
1000013
1000015
1000017
1000019
1000021
1000023
1000025
1000027
1000029
1000031
1000033 *** 0.06689453125
1000035
1000037 *** 0.06689453125 done
Exercise 1.21.
(smallest-divisor 199) = 199
(smallest-divisor 1999) = 1999
(smallest-divisor 19999) = 7
Exercise 1.19.
We have
\(a_{1}=(p+q)a_{0}+qb_{0}\)
\(b_{1}=qa_{0}+pb_{0}\)
So
\(a_{2}=(p+q)a_{1}+qb_{1}=(p^2+2pq+2q^2)a_{0}+(2pq+q^2)b_{0}\)
\(b_{2}=qa_{1}+pb_{1}=(q^2+2pq)a_{0}+(p^2+q^2)b_{0}\)
So we can infer that
\(p'\leftarrow p^2+q^2\)
\(q'\leftarrow 2pq+q^2\)
Exercise 1.18.
(define (multiply a b)
(define (multiply-iter q a b)
(cond
((= 1 b) (+ a q))
((even? b) (multiply-iter
q (double a) (halve b)))
(else (multiply-iter
(+ q a) a (- b 1)))))
(multiply-iter 0 a b))
Exercise 1.17.
(define (* a b)
(define (double x)
(+ x x))
(define (halve x)
(/ x 2))
(cond
((or (= 0 a) (= 0 b)) 0)
((= b 1) a)
((even? b) (* (double a) (halve b)))
(else (+ a (* a (- b 1))))))
Exercise 1.16.
(define (fast-expt b n)
(define (fast-expt-iter a b n)
(cond
((= 1 n) (* a b))
((even? n) (fast-expt-iter
a (* b b) (/ n 2)))
(else (fast-expt-iter
(* a b) b (- n 1)))))
Exercise 1.15.
a. \(\left\lceil\log_3{\frac{12.15}{0.1}}\right\rceil = 5\)
b. in general, it will takes \(\left\lceil\log_3{\frac{n}{0.1}}\right\rceil\) steps and \(2n\) in space.
the approximation
$$\lim_{x\to0} \frac{\sin{x}}{x} = 1$$
which can be proved by L'Hôpital's rule
trigonometric identity
can be infered by De Moivre's formula
Exercise 1.12.
(define (pascal-triangle-element n i)
(if (or (= i 0) (= i n) (< n 0))
1
(+
(pascal-triangle-element (- n 1) (- i 1))
(pascal-triangle-element (- n 1) i))))
Exercise 1.11.
recursive process
(define (f n)
(if (< n 3)
3
(+
(f (- n 1))
(* 2(f (- n 2)))
(* 3 (f (- n 3))))))
iterative process
(define (f n)
(f-iter 3 3 3 n))
(define (f-iter n1 n2 n3 count)
(if (= count 0)
n3
(f-iter
(+ n1 (* 2 n2) (* 3 n3))
n1
n2
(- count 1))))
Exercise 1.10.
First evaluation can infer that (A 1 n) = \(2^{n}\) .
(A 1 10)
(A 0 (A 1 9))
(* 2 (A 1 9))
(* 2 (A 0 (A 1 8)))
(* 2 (* 2 (A 1 8)))
(* 2 (* 2 (A 0 (A 1 7)))
(* 2 (* 2 (* 2 (A 1 7)))
(* 2 (* 2 (* 2 (A 0 (A 1 6)))
(* 2 (* 2 (* 2 (* 2 (A 1 6)))
(* 2 (* 2 (* 2 (* 2 (A 0 (A 1 5))))
(* 2 (* 2 (* 2 (* 2 (* 2 (A 1 5))))
(* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 4))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 1 4))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 3))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 1 3))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 2))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 1 2))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 0 (A 1 1))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (A 1 1))))
(* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 (* 2 2)))
1024
Second evaluation can infer that (A 2 n) = \(\underbrace{2^{2^{...^{2}}}}_{n}\) which is the Tetration written in \(^{4}2\)
(A 2 4)
(A 1 (A 2 3))
(A 1 (A 1 (A 2 2)))
(A 1 (A 1 (A 1 (A 2 1))))
(A 1 (A 1 (A 1 2)))
(A 1 (A 1 (A 0 (A 1 1))))
(A 1 (A 1 (A 0 2)))
(A 1 (A 1 (* 2 2)))
(A 1 (A 1 4))
(A 1 (A 0 (A 1 3)))
(A 1 (A 0 (A 0 (A 1 2))))
(A 1 (A 0 (A 0 (A 0 (A 1 1)))))
(A 1 (A 0 (A 0 (A 0 2))))
(A 1 (A 0 (A 0 (* 2 2))))
(A 1 (A 0 (A 0 4)))
(A 1 (A 0 (* 2 4)))
(A 1 (A 0 8))
(A 1 (* 2 8))
(A 1 16)
;... just as first evaluation above.
65536
So we can easily infer that (A 3 n) is the Pentation
(A 3 3)
(A 2 (A 3 2))
(A 2 (A 2 (A 3 1))
(A 2 (A 2 2)
;(A 2 (A 1 (A 2 1)))
;(A 2 (A 1 2))
;(A 2 (A 0 (A 1 1)))
;(A 2 (A 0 2))
;(A 2 (* 2 2))
(A 2 4)
65535
So the Ackermann's function can be infered
(A m n) = \(2[m+2]n\)
which is the Hyperoperation
Exercise 1.9.
First procedure is a linear recursive process.
(+ 4 5)
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9
While second procedure is a linear iterative process.
(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9
Our situation is analogous to that of someone who has learned the rules for how the pieces move in chess but knows nothing of typical openings, tactics, or strategy. Like the novice chess player, we don't yet know the common patterns of usage in the domain. We lack the knowledge of which moves are worth making (which procedures are worth defining). We lack the experience to predict the consequences of making a move (executing a procedure).
That is the most common case when we learn programming languages if the only thing we learn is the grammar.
The concept of consistent renaming is actually subtle and difficult to define formally. Famous logicians have made embarrassing errors here.
Can anyone explains this?
One detail of a procedure's implementation that should not matter to the user of the procedure is the implementer's choice of names for the procedure's formal parameters.
For example, when we define the good-enough? procedure in terms of square, we are able to regard the square procedure as a ``black box.'' We are not at that moment concerned with how the procedure computes its result, only with the fact that it computes the square. The details of how the square is computed can be suppressed, to be considered at a later time. Indeed, as far as the good-enough? procedure is concerned, square is not quite a procedure but rather an abstraction of a procedure, a so-called procedural abstraction. At this level of abstraction, any procedure that computes the square is equally good.
"black box" abstraction is something like "no testing in TDD"
Exercise 1.8.
(define (square x) (* x x))
(define (improve guess x)
(/ (+ (/ x (square guess)) (* guess 2)) 3))
(define (good-enough? guess next-guess)
(< (/ (abs (- guess next-guess)) guess) 0.001))
(define (cbrt-iter guess x)
(if (good-enough? guess (improve guess x))
guess
(cbrt-iter (improve guess x)
x)))
(define (cbrt x)
(cbrt-iter 1.0 x))
Exercise 1.7.
When a number x is so small that the precision 0.001 is too large to it relatively. So the best guess will be too rough. For example,
(square (sqrt 0.00009))
The result above is 0.0010370489120680158.
Due to the Loss of significanse, there always will be a fixed point) for improve procedure.
When the number is too large, the "best guess" will fixed in a number which precision is below 0.001.
So it will run into dead loop.
For example, 3162277.6601683795 is a fixed point for
(improve 3162277.6601683795 10000000000000)
while the result of (square 3162277.6601683795) is 10000000000000.002 which never be good enough as a guess but it is a fixed best guess.
Redefine good-enough? and sqrt-iter as below,
(define (good-enough? guess next-guess)
(< (/ (abs (- guess next-guess)) guess) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess (improve guess x))
guess
(sqrt-iter (improve guess x)
x)))
Exercise 1.5.
because scheme is applicative-order evaluation. So it will run into dead loop.
Exercise 1.6.
same reason. Scheme is applicative-order evaluation. So the new-if will run into dead loop.
Exercise 1.3.
(define (sum-square-of-max-two-numbers x y z)
(+
(square
(cond (
(and (>= x y) (>= x z)) x
)
(
(and (>= y x) (>= y z)) y
)
(
(and (>= z x) (>= z y)) z
)
)
)
(square
(cond (
(and (>= x y) (>= x z))
(if (>= y z) y z)
)
(
(and (>= y x) (>= y z))
(if (>= x z) x z)
)
(
(and (>= z x) (>= z y))
(if (>= x y) x y)
)
)
)
)
)
Considering the duplication of judging for max number.
(define (sum-square-of-max-two-numbers x y z)
(cond (
(and (>= x y) (>= x z))
(sum-square x (if (>= y z) y z))
)
(
(and (>= y x) (>= y z))
(sum-square y (if (>= x z) x z))
)
(
(and (>= z x) (>= z y))
(sum-square z (if (>= x y) x y))
)
)
Considering the commutative property of plus.
(define (sum-square-of-max-two-numbers x y z)
(cond (
(and (<= x y) (<= x z))
(sum-square y z)
)
(
(and (<= y x) (<= y z))
(sum-square x z)
)
(
(and (<= z x) (<= z y))
(sum-square x y)
)
)
Considering the duplication of judgement.
(define (sum-square-of-max-two-numbers x y z)
(if (and (<= x y) (<= x z))
(sum-square y z)
(sum-square-of-max-two-numbers y z x)
)
)
Exercise 1.2.
(/
(+ 5 1 + (- 2 (- 3 (+ 6 (/ 1 3)))))
(* 3 (- 6 2) (- 2 7))
)
Exercise 1.1.
10
12
8
3
6
19
#f
4
16
6
16
Ben Bitdiddle
Characters Several fictional characters appear in the book:
Over the course of this book, we will present a sequence of increasingly elaborate models of how interpreters work, culminating with a complete implementation of an interpreter and compiler in chapter 5.
book content squence.
A poor team will always create a poor system - it's very hard to tell if microservices reduce the mess in this case or make it worse.
All is about the people !
completed
Never completed.