1. Feng Zhou
  2. sicp-solutions

Commits

Feng Zhou  committed 18af727

changed file extension and deleted some files

  • Participants
  • Parent commits 1ee9464
  • Branches master

Comments (0)

Files changed (110)

File .DS_Store

Binary file added.

File ch01/.DS_Store

Binary file modified.

File ch01/ex-1.33.rkt

-;;; get ex1-31 right before attempting this question

File ch01/ex-1.33.scm

View file
+;;; get ex1-31 right before attempting this question

File ch01/ex1-1.rkt

-The results printed by the interpreter:
-
-10	; The value of 10 is 10.
-
-12	; The sum of 5, 3 and 4 is 12.
-
-8	; 9 minus 1 is 8.
-
-3	; 6 divided by 2 is 3.
-
-6	; The product of 2 and 4 is 8, 4 minus 6 is -2, the sum of 8 and -2 is 6
-
-a	; a is defined, its value is 3.
-
-b	; b is defined, its value is a + 1, which is 4.
-
-19	; a is 3, b is 4, the product of a and b is 12, the sum of 3 and 4 and 
-	; 12 is 19.
-
-#f	; false is returned, a is not equal to b.
-
-4	; If both predicates are true, return b, otherwise return a.
-
-16	; If a is equal to 4, return 6.
-	; If b is equal to 4, return the sum of 6, 7 and a(3).
-	; Else return 25.
-	; In this case, the sum of 6, 7 and a(3) is returned.
-
-16	; First, evaluate the cond, the second predicate is true, so b(4) is re-
-	; turned.
-	; Then, the product of 4 and (+ a 1) is 16 because a = 3.

File ch01/ex1-1.scm

View file
+The results printed by the interpreter:
+
+10	; The value of 10 is 10.
+
+12	; The sum of 5, 3 and 4 is 12.
+
+8	; 9 minus 1 is 8.
+
+3	; 6 divided by 2 is 3.
+
+6	; The product of 2 and 4 is 8, 4 minus 6 is -2, the sum of 8 and -2 is 6
+
+a	; a is defined, its value is 3.
+
+b	; b is defined, its value is a + 1, which is 4.
+
+19	; a is 3, b is 4, the product of a and b is 12, the sum of 3 and 4 and 
+	; 12 is 19.
+
+#f	; false is returned, a is not equal to b.
+
+4	; If both predicates are true, return b, otherwise return a.
+
+16	; If a is equal to 4, return 6.
+	; If b is equal to 4, return the sum of 6, 7 and a(3).
+	; Else return 25.
+	; In this case, the sum of 6, 7 and a(3) is returned.
+
+16	; First, evaluate the cond, the second predicate is true, so b(4) is re-
+	; turned.
+	; Then, the product of 4 and (+ a 1) is 16 because a = 3.

File ch01/ex1-11.rkt

-;;; Recursive version
-(define (f n)
-  (if (< n 3)
-      n
-      (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))
-
-;;; Iterative version
-(define (f2 n)
-  (f-iter 0 1 2 n))
-
-(define (f-iter a b c count)
-  (if (= count 0)
-      a
-      (f-iter b
-              c
-              (+ c (* 2 b) (* 3 a)) 
-              (- count 1))))

File ch01/ex1-11.scm

View file
+;;; Recursive version
+(define (f n)
+  (if (< n 3)
+      n
+      (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))
+
+;;; Iterative version
+(define (f2 n)
+  (f-iter 0 1 2 n))
+
+(define (f-iter a b c count)
+  (if (= count 0)
+      a
+      (f-iter b
+              c
+              (+ c (* 2 b) (* 3 a)) 
+              (- count 1))))

File ch01/ex1-12.rkt

-(define (pascal-tri row col)
-  (cond ((> col row) 0)
-        ((< col 0) 0)
-        ((= col 1) 1)
-        (else (+ (pascal-tri (- row 1) (- col 1))
-                 (pascal-tri (- row 1) col)))))

File ch01/ex1-12.scm

View file
+(define (pascal-tri row col)
+  (cond ((> col row) 0)
+        ((< col 0) 0)
+        ((= col 1) 1)
+        (else (+ (pascal-tri (- row 1) (- col 1))
+                 (pascal-tri (- row 1) col)))))

File ch01/ex1-16.rkt

-(define (expt b n)
-  (expt-iter 1 b n))
-
-(define (expt-iter a b n)
-  (cond ((= n 0) a)
-        ((even? n) (expt-iter (* a (square b))
-                              b
-                              (/ n 2)))
-        (else (expt-iter (* a b)
-                         b
-                         (- n 1)))))
-
-(define (square x)
-  (* x x))

File ch01/ex1-16.scm

View file
+(define (expt b n)
+  (expt-iter 1 b n))
+
+(define (expt-iter a b n)
+  (cond ((= n 0) a)
+        ((even? n) (expt-iter (* a (square b))
+                              b
+                              (/ n 2)))
+        (else (expt-iter (* a b)
+                         b
+                         (- n 1)))))
+
+(define (square x)
+  (* x x))

File ch01/ex1-2.rkt

-;;; Not sure how to properly indent this. One-liner or one element per line?
-;;; One line for numerator, another for denominator
-(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 3)))))
-   (* 3 (- 6 2) (- 2 7)))

File ch01/ex1-2.scm

View file
+;;; Not sure how to properly indent this. One-liner or one element per line?
+;;; One line for numerator, another for denominator
+(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 3)))))
+   (* 3 (- 6 2) (- 2 7)))

File ch01/ex1-3.rkt

-;;; A procedure that takes three numbers as arguments and returns the sum of the
-;;; tow larger numbers.
-
-;;; Tried to compute the smallest number and add the square of other two, didn't
-;;; work.
-
-(define (sum-of-sqr-of-largest-two a b c)
-  (cond ((and (< a b) (< a c)) (+ (square b) (square c)))
-        ((and (< b a) (< b c)) (+ (square a) (square c)))
-        ((and (< c a) (< c b)) (+ (square a) (square b)))))
-
-(define (square x)
-  (* x x))

File ch01/ex1-3.scm

View file
+;;; A procedure that takes three numbers as arguments and returns the sum of the
+;;; tow larger numbers.
+
+;;; Tried to compute the smallest number and add the square of other two, didn't
+;;; work.
+
+(define (sum-of-sqr-of-largest-two a b c)
+  (cond ((and (< a b) (< a c)) (+ (square b) (square c)))
+        ((and (< b a) (< b c)) (+ (square a) (square c)))
+        ((and (< c a) (< c b)) (+ (square a) (square b)))))
+
+(define (square x)
+  (* x x))

File ch01/ex1-30.rkt

-;;; I cheated a bit, the video lecture shows a similar procedure
-(define (sum term a next b)
-  (define (iter a result)
-    (if (> a b)
-        result
-        (iter (next a) (+ result (term a)))))
-  (iter a 0))

File ch01/ex1-30.scm

View file
+;;; I cheated a bit, the video lecture shows a similar procedure
+(define (sum term a next b)
+  (define (iter a result)
+    (if (> a b)
+        result
+        (iter (next a) (+ result (term a)))))
+  (iter a 0))

File ch01/ex1-31.rkt

-;;; wroing, need to debug
-(define (product term a next b)
-  (if (> a b)
-      0
-      (* (term a)
-         (product term (next a) next b))))
-
-(define (factorial n)
-  (product (lambda (x) x)
-           1
-           (lambda (x) (+ x 1))
-           n))

File ch01/ex1-31.scm

View file
+;;; wroing, need to debug
+(define (product term a next b)
+  (if (> a b)
+      0
+      (* (term a)
+         (product term (next a) next b))))
+
+(define (factorial n)
+  (product (lambda (x) x)
+           1
+           (lambda (x) (+ x 1))
+           n))

File ch01/ex1-32.rkt

-;;; get ex1-32 right before attempting this question.

File ch01/ex1-32.scm

View file
+;;; get ex1-32 right before attempting this question.

File ch01/ex1-34.rkt

-
-(define (f g)
-  (g 2))
-
-(define square
-  (lambda (x) (* x x)))
-
-(f square)
-
-(f (lambda (z) (* z (+ z 1))))
-
-(f f)
-
-;;; If we evaluate (f f), the interpreter gives erro message saying prcedure is 
-;;; required, but was given the number '2'.
-
-;;; What happens is that f is passed to f as argument, the the body (f 2) is 
-;;; evaluated. However, (f 2) is invalid because f expects a procedure for its 
-;;; argument, not a number.

File ch01/ex1-34.scm

View file
+
+(define (f g)
+  (g 2))
+
+(define square
+  (lambda (x) (* x x)))
+
+(f square)
+
+(f (lambda (z) (* z (+ z 1))))
+
+(f f)
+
+;;; If we evaluate (f f), the interpreter gives erro message saying prcedure is 
+;;; required, but was given the number '2'.
+
+;;; What happens is that f is passed to f as argument, the the body (f 2) is 
+;;; evaluated. However, (f 2) is invalid because f expects a procedure for its 
+;;; argument, not a number.

File ch01/ex1-35.rkt

-(define tolerance 0.00001)
-(define (fixed-point f first-guess)
-  (define (close-enough? v1 v2)
-    (< (abs (- v1 v2)) tolerance))
-  (define (try guess)
-    (let ((next (f guess)))
-      (if (close-enough? guess next)
-          next
-          (try next))))
-  (try first-guess))
-
-(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)

File ch01/ex1-35.scm

View file
+(define tolerance 0.00001)
+(define (fixed-point f first-guess)
+  (define (close-enough? v1 v2)
+    (< (abs (- v1 v2)) tolerance))
+  (define (try guess)
+    (let ((next (f guess)))
+      (if (close-enough? guess next)
+          next
+          (try next))))
+  (try first-guess))
+
+(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)

File ch01/ex1-36.rkt

-;;; Non done yet; this question requires ex1-22, which is not done. Will attempt
-;;; both exercises later.

File ch01/ex1-36.scm

View file
+;;; Non done yet; this question requires ex1-22, which is not done. Will attempt
+;;; both exercises later.

File ch01/ex1-4.rkt

-(a-plus-abs-b a b)
-
-;;; a plus the abs value of b
-;;; if b is larger than 0, the + operator is used to sum a and b.
-;;; if b is not larger than 0, the - operator is used to substract b from a.
-;;; This procedure only gets the absolute value of b; a retains its original va-
-;;; lue.

File ch01/ex1-4.scm

View file
+(a-plus-abs-b a b)
+
+;;; a plus the abs value of b
+;;; if b is larger than 0, the + operator is used to sum a and b.
+;;; if b is not larger than 0, the - operator is used to substract b from a.
+;;; This procedure only gets the absolute value of b; a retains its original va-
+;;; lue.

File ch01/ex1-41.rkt

-;;; not tested yet
-(define (double proc-f)
-  (lambda (x) (proc-f (proc-f x))))

File ch01/ex1-41.scm

View file
+;;; not tested yet
+(define (double proc-f)
+  (lambda (x) (proc-f (proc-f x))))

File ch01/ex1-42.rkt

-(define (compose f g)
-  (lambda (x) (f (g (x)))))

File ch01/ex1-42.scm

View file
+(define (compose f g)
+  (lambda (x) (f (g (x)))))

File ch01/ex1-43.rkt

-(define (compose f g)
-  (lambda (x) (f (g (x)))))
-
-(define (repeat f n)
-  (lambda (x) (compose (repeat f (- n 1)) f)))

File ch01/ex1-43.scm

View file
+(define (compose f g)
+  (lambda (x) (f (g (x)))))
+
+(define (repeat f n)
+  (lambda (x) (compose (repeat f (- n 1)) f)))

File ch01/ex1-5.rkt

-;; If the interpreter uses applicative-order evaluation, the evaluation steps are:
-;; (test 0 (p))
-;; (test 0 (p))
-;; (test 0 (p))
-;; ...
-;; because the interpreter evaluates the operands before the operator, so (p) is 
-;; called over and over again.
-
-
-;; If the interpreter uses normal-order evaluation, the evaluation steps are:
-;; (test 0 (p))
-;; 0
-;; 
-;; because the operands are not evaluated until they are needed, therefore the 
-;; first argument '0' is evaluated, which causes the procedure to return zero. 
-;; The second argument (p) is never needed, thus never evaluated.

File ch01/ex1-5.scm

View file
+;; If the interpreter uses applicative-order evaluation, the evaluation steps are:
+;; (test 0 (p))
+;; (test 0 (p))
+;; (test 0 (p))
+;; ...
+;; because the interpreter evaluates the operands before the operator, so (p) is 
+;; called over and over again.
+
+
+;; If the interpreter uses normal-order evaluation, the evaluation steps are:
+;; (test 0 (p))
+;; 0
+;; 
+;; because the operands are not evaluated until they are needed, therefore the 
+;; first argument '0' is evaluated, which causes the procedure to return zero. 
+;; The second argument (p) is never needed, thus never evaluated.

File ch01/ex1-6.rkt

-;;; The 'sqrt-iter defined with 'new-if works the same ways as the original 
-;;; 'sqrt-iter defined with 'if. The new-if re-defines if in a conditional mann-
-;;; er: if the predicate is true, the 'then-clause is executed, otherwise the '-
-;;; eles-clause is executed. 

File ch01/ex1-6.scm

View file
+;;; The 'sqrt-iter defined with 'new-if works the same ways as the original 
+;;; 'sqrt-iter defined with 'if. The new-if re-defines if in a conditional mann-
+;;; er: if the predicate is true, the 'then-clause is executed, otherwise the '-
+;;; eles-clause is executed. 

File ch01/ex1-7.rkt

-;; For very small numbers like 0.0001 the good-enough? procedure will always 
-;; return true because the difference between very small numbers is always very 
-;; small, thus the original version of good-enough? is not good enough.
-
-;; For very large numbers, if the precision is limited and larger than 0.001, 
-;; then it's possible that good-enough? will always return false because the 
-;; difference between two very large number might be always larger than 0.001.
-
-#lang racket
-(define (sqrt2 x)
-  (sqrt-iter 1.0 x))
-
-(define (sqrt-iter guess x)
-  (if (good-enough? guess (improve guess x))
-      (improve guess x)
-      (sqrt-iter (improve guess x)
-                 x)))
-
-(define (good-enough? guess next-guess)
-  (< (abs (- guess next-guess)) 0.001))
-
-(define (improve guess x)
-  (average guess (/ x guess)))
-
-(define (average a b)
-  (/ (+ a b) 2))
-
-; Test
-(sqrt2 0.0001)

File ch01/ex1-7.scm

View file
+;; For very small numbers like 0.0001 the good-enough? procedure will always 
+;; return true because the difference between very small numbers is always very 
+;; small, thus the original version of good-enough? is not good enough.
+
+;; For very large numbers, if the precision is limited and larger than 0.001, 
+;; then it's possible that good-enough? will always return false because the 
+;; difference between two very large number might be always larger than 0.001.
+
+#lang racket
+(define (sqrt2 x)
+  (sqrt-iter 1.0 x))
+
+(define (sqrt-iter guess x)
+  (if (good-enough? guess (improve guess x))
+      (improve guess x)
+      (sqrt-iter (improve guess x)
+                 x)))
+
+(define (good-enough? guess next-guess)
+  (< (abs (- guess next-guess)) 0.001))
+
+(define (improve guess x)
+  (average guess (/ x guess)))
+
+(define (average a b)
+  (/ (+ a b) 2))
+
+; Test
+(sqrt2 0.0001)

File ch01/ex1-9.rkt

-;;; Assuming applicative order
-;;; (+ 4 5)
-;;; (inc (+ (dec 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
-;;; The above process is linear recursive
-
-
-;;; Assuming applicative order
-;;; (+ 4 5)
-;;; (+ (dec 4) (inc 5))
-;;; (+ 3 6)
-;;; (+ (dec 3) (inc 6))
-;;; (+ 2 7)
-;;; (+ (dec 2) (inc 7)
-;;; (+ 1 8)
-;;; (+ (dec 1) (inc 8))
-;;; (+ 0 9)
-;;; 9
-;;; The above process is iterative 

File ch01/ex1-9.scm

View file
+;;; Assuming applicative order
+;;; (+ 4 5)
+;;; (inc (+ (dec 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
+;;; The above process is linear recursive
+
+
+;;; Assuming applicative order
+;;; (+ 4 5)
+;;; (+ (dec 4) (inc 5))
+;;; (+ 3 6)
+;;; (+ (dec 3) (inc 6))
+;;; (+ 2 7)
+;;; (+ (dec 2) (inc 7)
+;;; (+ 1 8)
+;;; (+ (dec 1) (inc 8))
+;;; (+ 0 9)
+;;; 9
+;;; The above process is iterative 

File ch01/pascal-tri.rkt

-(define (pascal-tri row col)
-  (cond ((> col row) 0)
-        ((< col 0) 0)
-        ((= col 1) 1)
-        (else (+ (pascal-tri (- row 1) (- col 1))
-                 (pascal-tri (- row 1) col)))))

File ch01/pascal-tri.scm

View file
+(define (pascal-tri row col)
+  (cond ((> col row) 0)
+        ((< col 0) 0)
+        ((= col 1) 1)
+        (else (+ (pascal-tri (- row 1) (- col 1))
+                 (pascal-tri (- row 1) col)))))

File ch01/sec2-1-examples.rkt

-(define x (cons 1 2))
-
-(car x)
-
-(cdr x)
-
-(define x (cons 1 2))
-(define y (cons 3 4))
-(define z (cons x y))
-
-(car (car z))
-
-(car (cdr z))

File ch01/sec2-1-examples.scm

View file
+(define x (cons 1 2))
+
+(car x)
+
+(cdr x)
+
+(define x (cons 1 2))
+(define y (cons 3 4))
+(define z (cons x y))
+
+(car (car z))
+
+(car (cdr z))

File ch01/sqrt-ex.rkt

-(define (new-if predicate then-clause eles-clause)
-  (cond (predicate then-clause)
-        (else else-clause)))
-
-(define (sqrt-iter guess x)
-  (new-if (good-enough? guess x)
-          guess
-          (sqrt-iter (improve guess x) 
-                     x)))
-
-(define (improve guess x)
-  (average guess (/ x guess)))
-
-(define (average x y)
-  (/ (+ x y) 2))
-
-(define (good-enough? guess x)
-  (< (abs (- (square guess) x)) 0.001))
-
-(define (sqrt x)
-  (sqrt-iter 1.0 x))
-
-(define (square x)
-  (* x x))
-
-(sqrt 9)

File ch01/sqrt-ex.scm

View file
+(define (new-if predicate then-clause eles-clause)
+  (cond (predicate then-clause)
+        (else else-clause)))
+
+(define (sqrt-iter guess x)
+  (new-if (good-enough? guess x)
+          guess
+          (sqrt-iter (improve guess x) 
+                     x)))
+
+(define (improve guess x)
+  (average guess (/ x guess)))
+
+(define (average x y)
+  (/ (+ x y) 2))
+
+(define (good-enough? guess x)
+  (< (abs (- (square guess) x)) 0.001))
+
+(define (sqrt x)
+  (sqrt-iter 1.0 x))
+
+(define (square x)
+  (* x x))
+
+(sqrt 9)

File ch02/2-17.rkt

-#lang racket
-; last-pair: returns the list that contains only the last element of a given list
-(define (last-pair items)
-  (if (null? (cdr items))
-      (car items)
-      (last-pair (cdr items))))

File ch02/2-17.rkt~

-(define (last-pair items)
-  (if (null? (cdr items))
-      (car items)
-      (last-pair (cdr items))))

File ch02/2-17.scm

View file
+#lang racket
+; last-pair: returns the list that contains only the last element of a given list
+(define (last-pair items)
+  (if (null? (cdr items))
+      (car items)
+      (last-pair (cdr items))))

File ch02/ex2-18.rkt

-#lang racket
-; reverse-2: takes a list as argument and returns a list of the same elements in reverse order.
-; It's name reverse-2 to distinguish it from the 'reverse' procedure that comes with racket. 
-
-(define (reverse-2 items)
-  (if (null? items)
-      '()
-      (append (reverse-2 (cdr items)) (list (car items)))))
-
-(reverse-2 (list 1 4 9 16 25))
-
-

File ch02/ex2-18.rkt~

-#lang racket
-; reverse: takes a list as argument and returns a list of the same elements in reverse order.
-
-(define (reserve items)
-  (if (null? (cdr items))
-      (car items)
-      (cons (cdr items) (car items))))
-
-(reverse (list 1 4 9))
-
-

File ch02/ex2-18.scm

View file
+#lang racket
+; reverse-2: takes a list as argument and returns a list of the same elements in reverse order.
+; It's name reverse-2 to distinguish it from the 'reverse' procedure that comes with racket. 
+
+(define (reverse-2 items)
+  (if (null? items)
+      '()
+      (append (reverse-2 (cdr items)) (list (car items)))))
+
+(reverse-2 (list 1 4 9 16 25))
+
+

File ch02/ex2-20.rkt

-(define (same-parity first . rest)
-  (cond (odd? (first)) (get-odd rest)
-        (even? (first)) (get-even rest)))
-
-(define (get-odd items)
-  (if (odd? (car items)) (car items)
-      (get-odd (cdr items))))
-
-(define (get-even items)
-  (if (even? (car items)) (car items)
-      (get-even (cdr items))))

File ch02/ex2-20.rkt~

-(define (same-parity first . rest)
-  (cond (odd? (first)) (get-odd rest)
-        (even? (first)) (get-even rest)))
-
-(define (get-odd items)
-  (if (odd? (car items)) (car items)
-      (get-odd (cdr items))))
-
-(define (get-even items)
-  (if (even? (car items)) (car items)
-      (get-even (cdr items))))

File ch02/ex2-20.scm

View file
+(define (same-parity first . rest)
+  (cond (odd? (first)) (get-odd rest)
+        (even? (first)) (get-even rest)))
+
+(define (get-odd items)
+  (if (odd? (car items)) (car items)
+      (get-odd (cdr items))))
+
+(define (get-even items)
+  (if (even? (car items)) (car items)
+      (get-even (cdr items))))

File ch02/ex2-21.rkt

-;;; Received error: "reference to undefined identifier: nil"
-;;; Doesn't racket define "nil"?
-(define (square-lite items)
-  (if (null? items)
-      nil
-      (cons (square (car items)) (square-lite (cdr items)))))
-
-(define (square-lite2 items)
-  (map (lambda (x) (square x)) items))
-
-(define (square x)
-  (* x x ))

File ch02/ex2-21.rkt~

-(define (square-lite items)
-  (if (null? items)
-      nil
-      (cons (square (car items)) (square-lite (cdr items)))))

File ch02/ex2-21.scm

View file
+;;; Received error: "reference to undefined identifier: nil"
+;;; Doesn't racket define "nil"?
+(define (square-lite items)
+  (if (null? items)
+      nil
+      (cons (square (car items)) (square-lite (cdr items)))))
+
+(define (square-lite2 items)
+  (map (lambda (x) (square x)) items))
+
+(define (square x)
+  (* x x ))

File ch02/ex2-22.rkt

-; The first procedure doesn't work because it takes the square of the first
-; element of cdr and cons it to the previous answer. The previous answer 
-; should be before the first element of cdr, but now is after.
-;
-; For example, consider (iter (list 1 2 3) nil):
-; (iter (list 1 2 3) nil)
-; (iter (list 2 3) (list 1 nil)
-; (iter (list 3) (list 4 1 nil))
-; (iter nil (list 9 4 1 nil))
-
-; The second procedure doesn't work because the parameter "answer" is a 
-; list, so the result becomes (list (list (list ...)))

File ch02/ex2-22.rkt~

-; The first procedure doesn't work because it takes the square of the first
-; element of cdr and cons it to the previous answer. The previous answer 
-; should be before the first element of cdr, but now is after.
-;
-; For example, consider (iter (list 1 2 3) nil):
-; (iter (list 1 2 3) nil)
-; (iter (list 2 3) (list 1 nil)
-; (iter (list 3) (list 4 1 nil))
-; (iter nil (list 9 4 1 nil))

File ch02/ex2-22.scm

View file
+; The first procedure doesn't work because it takes the square of the first
+; element of cdr and cons it to the previous answer. The previous answer 
+; should be before the first element of cdr, but now is after.
+;
+; For example, consider (iter (list 1 2 3) nil):
+; (iter (list 1 2 3) nil)
+; (iter (list 2 3) (list 1 nil)
+; (iter (list 3) (list 4 1 nil))
+; (iter nil (list 9 4 1 nil))
+
+; The second procedure doesn't work because the parameter "answer" is a 
+; list, so the result becomes (list (list (list ...)))

File ch02/ex2-23.rkt

-(define (for-each proc items)
-  (proc (car items))
-  (for-each proc (cdr items)))

File ch02/ex2-23.scm

View file
+(define (for-each proc items)
+  (proc (car items))
+  (for-each proc (cdr items)))

File ch02/ex2-24.rkt

-; Result of interpreter: (1 (2 (3 4)))
-; Box-and-pointer structure and interpretation as a tree are drawn by hand,
-; but I don't fully understand how it work, so most likely wrong.

File ch02/ex2-24.rkt~

-; Result of interpreter: (1 (2 (3 4)))

File ch02/ex2-24.scm

View file
+; Result of interpreter: (1 (2 (3 4)))
+; Box-and-pointer structure and interpretation as a tree are drawn by hand,
+; but I don't fully understand how it work, so most likely wrong.

File ch02/ex2-25.rkt

-; The result produced by this expression is '(7), still a list, why?
-(cdr (car (cdr (cdr (list 1 3 (list 5 7) 9)))))
-
-; The result produced by this expression is 7, not a list.
-(car (car (list (list 7))))
-
-; I tried this, but wrong. 
-(cdr (cdr (cdr (cdr (cdr (cdr (list 1 (list 2 (list 3 (list 4 (list 5 (list (6 7)))))))))))))

File ch02/ex2-25.rkt~

-; The result produced by this expression is '(7), still a list, why?
-(cdr (car (cdr (cdr (list 1 3 (list 5 7) 9)))))
-
-; The result produced by this expression is 7, not a list.
-(car (car (list (list 7))))

File ch02/ex2-25.scm

View file
+; The result produced by this expression is '(7), still a list, why?
+(cdr (car (cdr (cdr (list 1 3 (list 5 7) 9)))))
+
+; The result produced by this expression is 7, not a list.
+(car (car (list (list 7))))
+
+; I tried this, but wrong. 
+(cdr (cdr (cdr (cdr (cdr (cdr (list 1 (list 2 (list 3 (list 4 (list 5 (list (6 7)))))))))))))

File ch02/ex2-26.rkt

-(define x (list 1 2 3))
-(define y (list 4 5 6))
-
-
-; result of (append x y): '(1 2 3 4 5 6)
-
-; Don't fully understand this one
-; result of (cons x y): '((1 2 3) 4 5 6))
-
-; result of (list x y): '((1 2 3) (4 5 6))

File ch02/ex2-26.rkt~

-(define x (list 1 2 3))
-(define y (list 4 5 6))

File ch02/ex2-26.scm

View file
+(define x (list 1 2 3))
+(define y (list 4 5 6))
+
+
+; result of (append x y): '(1 2 3 4 5 6)
+
+; Don't fully understand this one
+; result of (cons x y): '((1 2 3) 4 5 6))
+
+; result of (list x y): '((1 2 3) (4 5 6))

File ch02/ex2-27.rkt

- need to finish ex2-18 before attampting this one

File ch02/ex2-27.rkt~

-; need to finish ex2-18 before attampting this one

File ch02/ex2-27.scm

View file
+ need to finish ex2-18 before attampting this one

File ch02/ex2-28.rkt

-; incomplete
-(define (fringe items)
-  (if (null? items)
-      nil
-      

File ch02/ex2-28.rkt~

-;

File ch02/ex2-28.scm

View file
+; incomplete
+(define (fringe items)
+  (if (null? items)
+      nil
+      

File ch02/ex2-38.rkt

-(define (fold-left op initial sequence)
-  (define (iter result rest)
-    (if (null? rest)
-        result
-        (iter (op result (car rest))
-              (cdr rest))))
-  (iter initial sequence))
-
-
-; still trying to write fold-right

File ch02/ex2-38.rkt~

-(define (fold-left op initial sequence)
-  (define (iter result rest)
-    (if (null? rest)
-        result
-        (iter (op result (car rest))
-              (cdr rest))))
-  (iter initial sequence))
-
-
-; still trying to write fold-right

File ch02/ex2-38.scm

View file
+(define (fold-left op initial sequence)
+  (define (iter result rest)
+    (if (null? rest)
+        result
+        (iter (op result (car rest))
+              (cdr rest))))
+  (iter initial sequence))
+
+
+; still trying to write fold-right

File ch02/ex2-4.rkt

-(define (cons x y)
-  (lambda (m) (m x y)))
-
-(define (car z)
-  (z (lambda (p q) p)))
-
-; Verification of (car z)
-; (car z)
-; (car (cons x y))
-; (lambda (lambda (p q) p))
-; (lambda (p q) x y)
-; (x)
-
-; Definition of cdr
-(define (cdr z)
-  (z (lambda (p q) q)))
-

File ch02/ex2-4.rkt~

-(define (cons x y)
-  (lambda (m) (m x y)))
-
-(define (car z)
-  (z (lambda (p q) p)))

File ch02/ex2-4.scm

View file
+(define (cons x y)
+  (lambda (m) (m x y)))
+
+(define (car z)
+  (z (lambda (p q) p)))
+
+; Verification of (car z)
+; (car z)
+; (car (cons x y))
+; (lambda (lambda (p q) p))
+; (lambda (p q) x y)
+; (x)
+
+; Definition of cdr
+(define (cdr z)
+  (z (lambda (p q) q)))
+

File ch02/ex2-53.rkt

-(list 'a 'b 'c)
-; => (a b c)
-
-(list (list 'george))
-; => ((george))
-
-(cdr '((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)

File ch02/ex2-53.scm

View file
+(list 'a 'b 'c)
+; => (a b c)
+
+(list (list 'george))
+; => ((george))
+
+(cdr '((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)

File ch02/ex2-54.rkt

-(equal? '(this is a list) '(this is a list))
-
-(equal? '(this is a list) '(this (is a) list))
-
-(define (equal? a b)
-  (cond ((and (not (pair? a)) (not (pair? b)) (eq? a b)) 
-         true)
-        ((and (pair? a) 
-              (pair? b) 
-              (eq? (car b) (car b))
-              (not (null? (cdr a)))
-              (not (null? (cdr b))))
-         (equal? (cdr a) (cdr b)))
-        (else false)))

File ch02/ex2-54.scm

View file
+(equal? '(this is a list) '(this is a list))
+
+(equal? '(this is a list) '(this (is a) list))
+
+(define (equal? a b)
+  (cond ((and (not (pair? a)) (not (pair? b)) (eq? a b)) 
+         true)
+        ((and (pair? a) 
+              (pair? b) 
+              (eq? (car b) (car b))
+              (not (null? (cdr a)))
+              (not (null? (cdr b))))
+         (equal? (cdr a) (cdr b)))
+        (else false)))

File ch02/ex2-55.rkt

-(car ''abracadabra)
-; => 'quote
-
-; The interpreter returs 'quote because the object quoted is 'abracadabra,
-; which is a quote itself, not a symbol.

File ch02/ex2-55.scm

View file
+(car ''abracadabra)
+; => 'quote
+
+; The interpreter returs 'quote because the object quoted is 'abracadabra,
+; which is a quote itself, not a symbol.

File ch02/notes.txt

-The word nil is a contraction of the Latin word nihil, which means "nothing".
-
-Data: some collection of selectors and constructors, together with specific 
-      conditions that these procedures must fulfill in order to be a valid 
-      representation.

File ch02/notes.txt~

-The word nil is a contraction of the Latin word nihil, which means "nothing".

File ch02/questions.txt

-1. Difference between "nil" and "null"? When to use which?
-
-2. Why does (cons (list 1 2) (list 3 4)) produces ((1 2) 3 4), not 
-   ((1 2) (3 4))

File ch02/questions.txt~

-1. Difference between "nil" and "null"?

File ch02/to-do.txt

-Exercises to do for chapter 2
-
-2.4
-2.17
-2.18
-2.20
-2.21
-2.22
-2.23
-2.24
-2.25
-2.26
-2.27
-2.28
-2.30
-2.21
-2.32
-2.33
-2.38
-2.39

File ch03/ex3-1.rkt

-(define (make-accumulator sum)
-  (lambda (a)
-    (set! sum (+ sum a))
-    sum))

File ch03/ex3-1.rkt~

-(define (make-accumulator sum)
-  (lambda (a)
-    (set! sum (+ sum a))
-    sum

File ch03/ex3-1.scm

View file
+(define (make-accumulator sum)
+  (lambda (a)
+    (set! sum (+ sum a))
+    sum))

File ch03/ex3-2.rkt

-
-(define (make-monitored f)
-  (let ((counter 0))
-    (define (mf m)
-      (cond ((eq? m 'how-many-calls?) counter)
-            ((eq? m 'reset-count) reset-count)
-            (else (begin (set! counter (+ counter 1))
-                         (f m)))))
-    (define reset-counter
-      (set! counter 0))
-    mf))
-

File ch03/ex3-2.rkt~

-(define (make-monitored f)
-  (let (counter 0)
-    (define (mf m)
-      (cond ((eq? m 'how-many-calls?) counter)
-            ((eq? m 'reset-count) reset-count)
-            (else (f m)
-                  (set! counter (+ counter 1)))))
-    (define reset-counter
-      (set! counter 0))
-    mf))

File ch03/ex3-2.scm

View file
+
+(define (make-monitored f)
+  (let ((counter 0))
+    (define (mf m)
+      (cond ((eq? m 'how-many-calls?) counter)
+            ((eq? m 'reset-count) reset-count)
+            (else (begin (set! counter (+ counter 1))
+                         (f m)))))
+    (define reset-counter
+      (set! counter 0))
+    mf))
+

File ch03/ex3.39.bak

-#lang racket
-
-;; 

File ch03/ex3.39.rkt

-#lang racket
-
-;; Possibilities 101, 121, 110 and 100 will remain. Because only the execution 
-;; part of p1 is serialized, so x can still be accessed concurrently, just 
-;; can't be modified.

File ch03/ex3.39.scm

View file
+#lang racket
+
+;; Possibilities 101, 121, 110 and 100 will remain. Because only the execution 
+;; part of p1 is serialized, so x can still be accessed concurrently, just 
+;; can't be modified.

File progress.org

-
-
-| section | read? | exercise | status |
-|---------+-------+----------+--------|
-|         |       |      1.1 | done   |
-|         |       |      1.2 | done   |
-|         |       |      1.3 | done   |
-|         |       |      1.4 | done   |
-|         |       |      1.5 | done   |
-|         |       |      1.6 | done   |
-|         |       |      1.7 | done   |
-|         |       |      1.8 |        |
-|         |       |      1.9 |        |
-|         |       |     1.10 |        |
-|         |       |     1.11 |        |
-|         |       |     1.12 |        |
-|         |       |     1.13 |        |
-|         |       |     1.14 |        |
-|         |       |     1.15 |        |
-|         |       |     1.16 |        |
-|         |       |     1.17 |        |
-|         |       |     1.18 |        |
-|         |       |     1.19 |        |
-|         |       |     1.20 |        |
-|         |       |     1.21 |        |
-|         |       |     1.22 |        |
-|         |       |     1.23 |        |
-|         |       |     1.24 |        |
-|         |       |     1.25 |        |
-|         |       |     1.26 |        |
-|         |       |     1.27 |        |
-|         |       |     1.28 |        |
-|         |       |     1.29 |        |
-|         |       |     1.30 |        |
-|         |       |     1.31 |        |
-|         |       |     1.32 |        |
-|         |       |     1.33 |        |
-|         |       |     1.34 |        |
-|         |       |     1.35 |        |
-|         |       |     1.36 |        |
-|         |       |     1.37 |        |
-|         |       |     1.38 |        |
-|         |       |     1.39 |        |
-|         |       |     1.40 |        |
-|         |       |     1.41 |        |
-|         |       |     1.42 |        |
-|         |       |     1.43 |        |
-|         |       |     1.44 |        |
-|         |       |     1.45 |        |
-|         |       |     1.46 |        |
-

File progress.org~

-
-
-| Exercise | status |
-|----------+--------|
-| 1.1      |        |
-| 1.2      |        |
-| 1.3      |        |
-| 1.4      |        |
-| 1.5      |        |
-| 1.6      |        |
-| 1.7      |        |
-| 1.8      |        |
-| 1.9      |        |
-| 1.10     |        |
-| 1.11     |        |
-| 1.12     |        |
-| 1.13     |        |
-| 1.14     |        |
-| 1.15     |        |
-| 1.16     |        |
-| 1.17     |        |
-| 1.18     |        |
-| 1.19     |        |
-| 1.20     |        |
-| 1.21     |        |
-| 1.22     |        |
-| 1.23     |        |
-| 1.24     |        |
-| 1.25     |        |
-| 1.26     |        |
-| 1.27     |        |
-| 1.28     |        |
-| 1.29     |        |
-| 1.30     |        |
-| 1.31     |        |
-| 1.32     |        |
-| 1.33     |        |
-| 1.34     |        |
-| 1.35     |        |
-| 1.36     |        |
-| 1.37     |        |
-| 1.38     |        |
-| 1.39     |        |
-| 1.40     |        |
-| 1.41     |        |
-| 1.42     |        |
-| 1.43     |        |
-| 1.44     |        |
-| 1.45     |        |
-| 1.46     |        |