BLOG ARTICLE 분류 전체보기 | 46 ARTICLE FOUND

  1. 2011.01.05 연습문제 2.6
  2. 2011.01.05 연습문제 2.4 ~ 2.5
  3. 2011.01.05 2.1.3
  4. 2011.01.01 SICP github repository
  5. 2011.01.01 연습문제 2.2 ~ 2.3
  6. 2011.01.01 2.1.2
  7. 2011.01.01 연습문제 2.1
  8. 2011.01.01 2.1.1
  9. 2011.01.01 Chapter.2 intro
  10. 2010.12.28 연습문제 1.38, 연습문제 1.39

; ex_2_6

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
  (lambda (f) (lambda (x) (f ((n f) x)))))

; one
;(add-1 zero)
;(lambda (f) (lambda (x) (f ((zero f) x))))
;;((zero f) x)
;;((lambda (x) x) x)
;;x
;(lambda (f) (lambda (x) (f x)))
(define one (lambda (f) (lambda (x) (f x))))

; two
;(add-1 one)
;(lambda (f) (lambda (x) (f ((one f) x))))
;((one f) x)
;(((lambda (a) (lambda (b) (a b))) f) x)
;((lambda (b) (f b)) x)
;(f x)
;(lambda (f) (lambda (x) (f (f x))))
(define two (lambda (f) (lambda (x) (f (f x)))))

; three
(define three (lambda (f) (lambda (x) (f (f (f x))))))

; add
;(define (add a b) (lambda (f) (lambda (x) ("?"))))
;suppose a = 3, b = 3
;("?")
;(fa (fa (fa (fb (fb (fb x))))))
;(fa (fa (fa ((b f) x))))
;((a f) ((b f) x))

(define (add a b)
  (lambda (f)
    (lambda (x)
      ((a f) ((b f) x)))))
AND

; ex 2.4

; ex 2.5

(define (power x n)
  (define (power-iter r c)
    (if (= c 0)
        r
        (power-iter (* r x) (- c 1))))
  (power-iter 1 n))

(define (cons a b)
  (lambda (m) (m a b)))

(define (car z)
  (z (lambda (p q) (power 2 p))))

(define (cdr z)
  (z (lambda (p q) (power 3 q))))
AND

2.1.3

Announce/Chapter.2 2011. 1. 5. 22:30
; 2.1.3

(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
          ((= m 1) y)
          (else (error "error"))))
  dispatch)

(define (car z) (z 0))

(define (cdr z) (z 1))
AND

SICP github repository

Reference 2011. 1. 1. 21:58

티스토리에 소스를 올리는게 넘 불편해서 github repository를 하나 만들었습니다. 앞으로 소스 코드는 여기에 올리는게 어떨지? 이참에 분산 VCSgit에 대해서도 알아보는건? ^^

커밋을 하려면 github에 가입하고도, 몇 가지 준비가 필요하니 한번 논의해 보시죠.

AND

; ex 2.2

; point
(define (make-point x y) (cons x y))
(define (x-point p) (car p))
(define (y-point p) (cdr p))
(define (point2str p)
  (format "(~a,~a)"
          (x-point p) (y-point p)))
(define (print-point p)
  (newline)
  (display (point2str p)))

; segment
(define (make-segment p1 p2) (cons p1 p2))
(define (start-segment s) (car s))
(define (end-segment s) (cdr s))
(define (segment2str s)
  (format "~a->~a"
          (point2str (start-segment s))
          (point2str (end-segment s))))
(define (print-segment s)
  (newline)
  (display (segment2str s)))

(define (midpoint-segment s)
  (define (average a b) (/ (+ a b) 2))
  (let ((p1 (start-segment s))
        (p2 (end-segment s)))
    (make-point (average (x-point p1) (x-point p2))
                (average (y-point p1) (y-point p2)))))

; ex 2.3

; rect

(define (make-rect tl-point br-point) (cons tl-point br-point))
;(define (make-rect p1 p2) (cons p1 p2))
(define (top-left-rect r) (car r))
(define (top-right-rect r)
  (make-point (x-point (cdr r)) (y-point (car r))))
(define (bottom-left-rect r)
  (make-point (x-point (car r)) (y-point (cdr r))))
(define (bottom-right-rect r) (cdr r))
;(define (top-left-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (min x1 x2) (max y1 y2))))
;(define (top-right-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (max x1 x2) (max y1 y2))))
;(define (bottom-left-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (min x1 x2) (min y1 y2))))
;(define (bottom-right-rect r)
;  (let ((x1 (x-point (car r)))
;        (y1 (y-point (car r)))
;        (x2 (x-point (cdr r)))
;        (y2 (y-point (cdr r))))
;    (make-point (max x1 x2) (min y1 y2))))
(define (rect2str r)
  (format "tl=~a\ntr=~a\nbl=~a\nbr=~a"
          (top-left-rect r)
          (top-right-rect r)
          (bottom-left-rect r)
          (bottom-right-rect r)))
(define (print-rect r)
  (newline)
  (display (rect2str r)))

(define (width-rect r)
  (- (x-point (top-right-rect r)) (x-point (top-left-rect r))))
(define (height-rect r)
  (- (y-point (top-left-rect r)) (y-point (bottom-left-rect r))))
(define (area-rect r)
  (* (width-rect r) (height-rect r)))
(define (girth-rect r)
  (+ (* (width-rect r) 2) (* (height-rect r) 2)))
AND

2.1.2

Announce/Chapter.2 2011. 1. 1. 11:07
...
AND

; ex_2_1

(define (make-rat3 n d)
  (let ((g ((if (< d 0) - +) (gcd n d))))
    (cons (/ n g) (/ d g))))
AND

2.1.1

Announce/Chapter.2 2011. 1. 1. 11:05
; 2.1.1

; operation
(define (add-rat x y)
  (make-rat (+ (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))
(define (sub-rat x y)
  (make-rat (- (* (numer x) (denom y))
               (* (numer y) (denom x)))
            (* (denom x) (denom y))))
(define (mul-rat x y)
  (make-rat (* (numer x) (numer y))
            (* (denom x) (denom y))))
(define (div-rat x y)
  (make-rat (* (numer x) (denom y))
            (* (numer y) (denom x))))
(define (equal-rat? x y)
  (= (* (numer x) (denom y))
     (* (numer y) (denom x))))

; constructor, selector
(define (make-rat n d) (cons n d))
(define (numer x) (car x))
(define (denom x) (cdr x))

(define (print-rat x)
  (newline)
  (display (numer x))
  (display "/")
  (display (denom x)))

(define (make-rat2 n d)
  (let ((g (gcd n d)))
    (cons (/ n g) (/ d g))))
AND

...
AND


(define (cont-frac n d k)
  (define (cont-frac-helper i)
    (if (> i k)
        0
        (/ (n i) (+ (d i) (cont-frac-helper (+ i 1))))))
  (cont-frac-helper 1))

(define (ln-cf k)
  (define (n k) 1.0)
  (define (d k) (if (= (remainder (+ k 1) 3) 0) (* (/ (+ k 1) 3) 2) 1))
  (cont-frac n d k))

(ln-cf 10)

(define (tan-cf x k)
  (define (n k) (if ( = k 1) x (* x x)))
  (define (d k) (- (* 2 k) 1))
  (cont-frac n d k))

(tan-cf 0 10)
환영합니다. DrRacket, 버전 5.0.2 [3m].
언어: R5RS [사용자정의]; memory limit: 256 MB.
0.7182817182817183
0
>
AND