Tag: Lisp

sicp-4-3-3

Posted on 2015-03-16 21:02:58 +0900 in SICP Lisp

4.51 It would always be (a b 1). 4.52 (define (analyze-if-fail exp) (let ((first (analyze (cadr exp))) (second (analyze (caddr exp)))) (lambda (env succeed fail) (first env (lambda (value fail2) (succeed value fail2)) (lambda () (second env succeed fail)))))) 4.53 (let ((pairs '())) (if-fail (let ((p (prime-sum-pair '(1 3 5...

Read more...

sicp-4-3-2

Posted on 2015-03-15 06:36:17 +0900 in SICP Lisp

4.39 (require (> miller cooper)) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1))) (require (not (= (abs (- smith fletcher)) 1))) (require (distinct? (list baker cooper fletcher miller smith)))(require (not (= (abs (- fletcher cooper)) 1))) The answer...

Read more...

sicp-4-3-1

Posted on 2015-03-13 22:47:34 +0900 in SICP Lisp

I just find 4.3 very hard to understand. Eventhough I followed the author’s ideas, and find it work. But this time I found even try to understand the callings through debug would be very difficult. There should be theoretical foundations for this, finally, though this tutorial, I found the idea...

Read more...

sicp-4-2-3

Posted on 2015-03-12 20:27:03 +0900 in SICP Lisp

4.32 In the current version, car is lazy as well, which makes it possible to build structures such as infinite tree. 4.33 ;'(a b c)-->(cons 'a (cons 'b (cons 'c '()))) ((quoted? exp) (text-of-quotation exp env)) (define (list->cons lst) (if (null? lst) '() (list 'cons (list 'quote (car lst)) (list->cons...

Read more...

sicp-4-2-1

Posted on 2015-03-11 19:20:01 +0900 in SICP Lisp

4.25 (define (unless condition usual-value exceptional-value) (if condition exceptional-value usual-value)) (define (factorial n) (unless (= n 1) (* n (factorial (- n 1))) 1)) In applicative-order, factorial will enter an infinite loop, as the exceptional-value is evaluated regardless the condition. While in normal-order, the condition is judged before exceptional-value is...

Read more...

sicp-4-1-6-4-1-7

Posted on 2015-03-09 23:37:54 +0900 in SICP Lisp

4.16 a: (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (if (equal? (car vals) '*unassigned*) (error "Unassigned variable" var) (car vals))) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame...

Read more...

sicp-4-1-3-4-1-5

Posted on 2015-03-06 20:12:10 +0900 in SICP Lisp

4.11 (define (make-frame variables values) (if (= (length variables) (length values)) (map cons variables values) (error "length mismatch -- MAKE-FRAME" variables values))) (define (frame-variables frame) (map car frame)) (define (frame-values frame) (map cdr frame)) 4.12 I can only come up with a find-binding-frame function and manually controls its process under...

Read more...

sicp-4-1

Posted on 2015-03-06 00:51:11 +0900 in SICP Lisp

4.1 It takes me several minutes to really understand the quanstion. :( It really only has something to do with the order of the parameter evaluation. To restrict the order, the parameters passed in to cons should be taken out explicitly before being used. (define (list-of-values-left-right exps env) (if (no-operands...

Read more...

sicp-3-5-4-3-5-5

Posted on 2015-03-05 06:11:43 +0900 in SICP Lisp

3.77 (define (integral delayed-integrand initial-value dt) (cons-stream initial-value (let ((integrand (force delayed-integrand))) (if (stream-null? integrand) the-empty-stream (integral (delay (stream-cdr integrand)) (+ (* dt (stream-car integrand)) initial-value) dt))))) 3.78 (define (integral delayed-integrand initial-value dt) (cons-stream initial-value (let ((integrand (force delayed-integrand))) (if (stream-null? integrand) the-empty-stream (integral (delay (stream-cdr integrand)) (+ (* dt...

Read more...

sicp-3-5-3

Posted on 2015-03-03 22:24:29 +0900 in SICP Lisp

3.63 guess is used to share the computation result between the succssive calling. Otherwise each call to sqrt-stream x would require recursively calling. If memo-proc is not used, these two versions would be the same efficiency. 3.64 (define (stream-limit s tolerance) (let ((s2 (stream-cdr s))) (if (< (abs (- (stream-car...

Read more...

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

Posted on 2015-03-02 20:24:25 +0900 in SICP Lisp

Code from the text. (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s))))) (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car s)) (stream-for-each proc...

Read more...

sicp-3-4

Posted on 2015-03-02 01:03:22 +0900 in SICP Lisp

3.39 101: P1 , P2 121: P2, P1 100: P1 computes (* x x), then P2 completes and sets x to 101, then P1 executes the assignment. 3.41 As withdraw and diposit are both single modification operations, it is not necessary to protect the balance value, as it would always...

Read more...

sicp-3-3-5

Posted on 2015-03-01 22:37:18 +0900 in SICP Lisp

3.33 (define (average a b c) (let ((x (make-connector)) (y (make-connector))) (adder a b x) (multiplier c v u) (constant 2 v)) 'ok) 3.34 it is only one-directianl computation. Given the value of b, the value of a can not be evaluated, as the multiplier does not know the left...

Read more...

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...