Archives for February 2015

sicp-3-3-4

Posted on 2015-02-28 07:36:07 +0900 in SICP Lisp

3.28 (define (or-gate a1 a2 output) (define or-action-procedure (let ((new-value (logical-or (get-signal a1) (get-signal a2)))) (after-delay or-gate-delay (lambda () set-signal! output new_value)))) (add-action! a1 or-action-procedure) (add-action! a2 or-action-procedure) 'ok) 3.29 (define (or-gate a1 a2 output) (define (or-action-procedure) (let ((not-a1 (make-wire)) (not-a2 (make-wire)) (b (make-wire)) ) (inverter a1 not-a1) (inverter a2...

Read more...

sicp-3-3-3

Posted on 2015-02-27 19:57:55 +0900 in SICP Lisp

3.24 (define (make-table same-key?) (let ((local-table (list '*table*))) (define (assoc key records) (cond ((null? records) #f) ((same-key? key (caar records)) (car records)) (else (assoc key (cdr records))))) (define (lookup key-1 key-2) (let ((subtable (assoc key-1 (cdr local-table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (cdr record) #f...

Read more...

sicp-3-3-2

Posted on 2015-02-27 00:17:13 +0900 in SICP Lisp

3.21 (define (front-ptr queue) (car queue)) (define (rear-ptr queue) (cdr queue)) (define (set-front-ptr! queue item) (set-car! queue item)) (define (set-rear-ptr! queue item) (set-cdr! queue item)) (define (empty-queue? queue) (null? (front-ptr queue))) (define (make-queue) (cons '() '())) (define (front-queue queue) (if (empty-queue? queue) (error "FRONT called with an empty queue" queue)...

Read more...

sicp-3-3-1

Posted on 2015-02-26 20:23:25 +0900 in SICP Lisp

3.16 (define (count-pairs x) (if (not (pair? x)) 0 (+ (count-pairs (car x)) (count-pairs (cdr x)) 1))) (define z3 '(a b c)) (count-pairs z3) (define z1 '(c)) (define z4 (list z1 z1)) (define z23 (cons z1 z1)) (define z7 (cons z23 z23)) (define zi '(a b c)) (set-cdr! (cddr zi)...

Read more...

sicp-3-2

Posted on 2015-02-26 07:59:06 +0900 in SICP Lisp

In the environment model of evaluation, a procedure is always a pair consisting of some code and a pointer to an environment. Procedures are created in one way only: by evaluating a lambda expression. This produces a procedure whose code is obtained from the text of the lambda expression and...

Read more...

sicp-3-1-2-3-1-3

Posted on 2015-02-26 01:59:04 +0900 in SICP Lisp

3.5 #lang racket (define (square x) (* x x)) (define (random-in-range low high) (let ((range (- high low))) (+ low (* (random) range)))) (define (monte-carlo trials experiment) (define (iter trials-remaining trials-passed) (cond ((= trials-remaining 0) (/ trials-passed trials)) ((experiment) (iter (- trials-remaining 1) (+ trials-passed 1))) (else (iter (- trials-remaining...

Read more...

sicp-3-1-1

Posted on 2015-02-25 20:47:35 +0900 in SICP Lisp

3.1 (define (make-accumulator initial) (let ((accumulate initial)) (lambda (x) (set! accumulate (+ accumulate x)) accumulate))) 3.2 (define (make-monitored proc) (let ((call-times 0)) (define (dispatch m) (cond ((eq? m 'how-many-calls?) call-times) ((eq? m 'reset-count) (set! call-count 0)) (else (set! call-times (+ 1 call-times)) (proc m)))) dispatch)) 3.3 (define (make-account balance password)...

Read more...

sicp-2-5-2

Posted on 2015-02-25 07:03:37 +0900 in SICP Lisp

2.81 If try to coerce two numbers, it would fall into a infinite loop. It would show an error message if trying to convert an augument to its own type, which is correct. 2.82 (define (apply-generic op . args) (define (apply-generic op args type-list) (if (null? type-list) (error "No method...

Read more...

sicp-2-5-1

Posted on 2015-02-24 21:01:24 +0900 in SICP Lisp

2.77 (apply-generic 'magnitude '(complex rectangular 3 4)) --> ((get 'magnitude '(complex)) '(rectangular 3 4)) --> (magnitude '(rectangular 3 4)) --> (apply-generic 'magnitude '(rectangular 3 4)) --> ((get 'magnitude '(rectangular)) (3 4)) --> (magnitude (3 4)) 2.78 (define (attach-tag type-tag contents) (if (number? contents) contents (cons type-tag contents))) (define (type-tag datum)...

Read more...

sicp-2-4

Posted on 2015-02-24 06:45:38 +0900 in SICP Lisp

2.73 (define global-array '()) (define (make-entry k v) (list k v)) (define (key entry) (car entry)) (define (value entry) (cadr entry)) (define (put op type item) (define (put-helper k array) (cond ((null? array) (list(make-entry k item))) ((equal? (key (car array)) k) array) (else (cons (car array) (put-helper k (cdr array))))))...

Read more...

sicp-2-3-4

Posted on 2015-02-23 02:20:49 +0900 in SICP Lisp

2.67 (define (make-leaf symbol weight) (list 'leaf symbol weight)) (define (leaf? object) (eq? (car object) 'leaf)) (define (symbol-leaf x) (cadr x)) (define (weight-leaf x) (caddr x)) (define (make-code-tree left right) (list left right (append (symbols left) (symbols right)) (+ (weight left) (weight right)))) (define (left-branch tree) (car tree)) (define (right-branch...

Read more...

sicp-2.3.3

Posted on 2015-02-22 23:54:30 +0900 in SICP Lisp

2.59 (define (union-set set1 set2) (cond ((null? set1) set2) ((element-of-set? (car set1) set2) (union-set (cdr set1) set2)) (else (cons (car set1) (union-set (cdr set1) set2))))) 2.60 (define (adjoin-set x set) (cons x set)) (define (union-set set1 set2) (append set1 set2)) adjoin-set goes from $O(n)$ to $O(1)$ union-set goes from $O(n^2)$...

Read more...

sicp-2.3.2

Posted on 2015-02-21 07:41:20 +0900 in SICP Lisp

2.56 (print-as-expression #f) (define (variable? x) (symbol? x)) (define (same-variable? v1 v2) (and (variable? v1) (variable? v2) (eq? v1 v2))) (define (=number? exp num) (and (number? exp) (= exp num))) (define (make-sum a1 a2) (cond ((=number? a1 0) a2) ((=number? a2 0) a1) ((and (number? a1) (number? a2)) (+ a1...

Read more...

sicp-2.3.1-2.3.2

Posted on 2015-02-20 19:41:41 +0900 in SICP Lisp

2.54 (define (equal? a b) (cond ((and (pair? a) (pair? b)) (and (equal? (car a) (car b)) (equal? (cdr a) (cdr b)))) ((and (not (pair? a)) (not (pair? b))) (eq? a b)) (else #f))) 2.55 (car ''abracadabra) --> (car (quote (quote abracadabra))) --> 'quote

Read more...

sicp-2.2.4

Posted on 2015-02-20 07:20:40 +0900 in SICP Lisp

package used Drracket picture language is used. #lang racket (require (planet "sicp.ss" ("soegaard" "sicp.plt" 2 1))) 2.44 (define (up-split painter n) (if (= n 0) painter (let ((smaller (up-split painter (- n 1)))) (below painter (beside smaller smaller))))) 2.45 (define (split op1 op2) (lambda (painter n) (if (= n 0)...

Read more...

sicp-2.2.3

Posted on 2015-02-18 19:31:27 +0900 in SICP Lisp

2.33 (define (accumulate op initial sequence) (if (null? sequence) initial (op (car sequence) (accumulate op initial (cdr sequence))))) (accumulate + 0 (list 1 2 3 4 5)) (define (map p sequence) (accumulate (lambda (x y) (cons (p x) y)) null sequence)) (define (square x) (* x x)) (map square '(1...

Read more...

sicp-2-2-2

Posted on 2015-02-17 20:56:36 +0900 in SICP Lisp

2.24 The interpreter would show (1 (2 (3 4))). 2.25 (cadr (caddr '(1 3 (5 7) 9))) (caar '((7))) (cadr (cadr (cadr (cadr (cadr (cadr '(1 (2 (3 (4 (5 (6 7)))))))))))) 2.26 (define x (list 1 2 3)) (define y (list 4 5 6)) (append x y) -> (1...

Read more...

sicp-2.2.1

Posted on 2015-02-17 07:42:44 +0900 in SICP Lisp

2.17 (define (last-pair lst) (let ([second (cdr lst)]) (if (null? second) (car lst) (last-pair second)))) 2.18 (define (append list1 list2) (if (null? list1) list2 (cons (car list1) (append (cdr list1) list2)))) (define (reverse lst) (if (null? lst) lst (append (reverse (cdr lst)) (list (car lst))))) 2.19 (define us-coins (list 50...

Read more...

sicp-2.1.4

Posted on 2015-02-17 06:45:26 +0900 in SICP Lisp

2.7 (define (add-interval x y) (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y)))) (define (mul-interval x y) (let ((p1 (* (lower-bound x) (lower-bound y))) (p2 (* (lower-bound x) (upper-bound y))) (p3 (* (upper-bound x) (lower-bound y))) (p4 (* (upper-bound x) (upper-bound y)))) (make-interval (min p1 p2 p3...

Read more...

sicp-2.1.1-2.1.3

Posted on 2015-02-16 19:31:58 +0900 in SICP Lisp

2.1 (define (number x) (car x)) (define (denom x) (cdr x)) (define (make-rat n d) (let ((g (gcd n d))) (if (> d 0) (cons (/ n g) (/ d g)) (cons (/ (- n) g) (/ (- d) g))))) (define (print-rat x) (newline) (display (number x)) (display "/") (display...

Read more...

1.3.4

Posted on 2015-02-16 00:39:53 +0900 in SICP Lisp

In general, programming languages impose restrictions on the ways in which computational elements can be manipulated. Elements with the fewest restrictions are said to have first-class status. Some of the ``rights and privileges’’ of first-class elements are: They may be named by variables. They may be passed as arguments to...

Read more...

SICP 1.3.2-1.3.3

Posted on 2015-02-15 20:13:00 +0900 in SICP Lisp

1.34 (define (f g) (g 2)) (f f) --> (f 2) --> (2 2) 1.35 (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...

Read more...

sicp-1.31

Posted on 2015-02-14 08:22:48 +0900 in SICP Lisp

1.29 (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) (define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (define (cube x) (* x x...

Read more...

sicp-126

Posted on 2015-02-13 19:50:38 +0900 in SICP Lisp

1.21 (define (square x) (* x x)) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) 199 and 1999 are primes, 19999’s smallest divisor...

Read more...

sicp-124-125

Posted on 2015-02-12 23:17:32 +0900 in SICP Lisp

1.16 #lang planet neil/sicp (define (square x) (* x x)) (define (fast-expt b n) (define (iter a b n) (cond ((= n 0) a) ((even? n) (iter a (square b) (/ n 2))) (else (iter (* a b) b (- n 1))))) (iter 1 b n)) 1.18 (define (multi a...

Read more...

SICP 1.2.3

Posted on 2015-02-12 20:42:17 +0900 in SICP Lisp

Lisp

Read more...

SICP 1.2.2

Posted on 2015-02-12 19:25:25 +0900 in SICP Lisp

Lisp

Read more...

SICP 1.2.1

Posted on 2015-02-12 07:59:53 +0900 in SICP Lisp

Lisp

Read more...

SICP section 1.1

Posted on 2015-02-12 04:51:53 +0900 in SICP Lisp

Elements of Programming

Read more...

说好的2014

Posted on 2015-02-09 19:27:53 +0900 in Life Review

说好的2014

Read more...