;; exercises 1.46

(define (iterative-improve f enough? first-guess)
  (define (iter guess)
    (if (enough? guess)
        guess
        (iter (f guess))))
  (iter first-guess))

(define (sqrt-iter-imp x)
  (iterative-improve (lambda (guess) (average guess (/ x guess)))
                     (lambda (guess) (< (abs (- (square guess) x)) 0.001))
                     1.0))
(define (fixed-point-iter-imp f first-guess)
  (iterative-improve f
                     (lambda (guess) (< (abs (- guess (f guess))) 0.001))
                     first-guess))

이건 내꺼

;; ex 1.46 - iterative-improve
(define (iterative-improve good? improve)
  (lambda (guess)
    (define (iter v)
      (if (good? v)
          v
          (iter (improve v))))
    (iter guess)))

;; sqrt
(define (sqrt-ii x)
  (define (good? guess)
    (< (abs (- (square guess) x)) 0.001))
  (define (improve guess)
    (/ (+ guess (/ x guess)) 2))
  ((iterative-improve good? improve) 1.0))

(sqrt-ii 9)

;; fixed-point
(define (fixed-point-ii f g)
  (define (good? guess)
    (< (abs (- guess (f guess)) 0.00001)))
  ((iterative-improve good? f) g))

(fixed-point cos 1.0)
신고

HTML 편집 모드에서 아래와 같이 <pre class="brush:scheme"></pre> 사이에 Scheme 코드를 입력하면 예쁘게 출력된다.

입력

<pre class="brush:scheme">
;; ex 1.12 pascal's triangle
;; p(r, c) = p(r-1, c-1) + p(r-1, c)
;; p(r, 1) = 1, p(r, r) = 1
(define (pt r c)
  (cond ((or (= c 1) (= r c)) 1)
        ((or (< c 0) (< r 0) (> c r)) 0)
        (else (+ (pt (- r 1) (- c 1))
                 (pt (- r 1) c)))))
</pre>

출력

;; ex 1.12 pascal's triangle
;; p(r, c) = p(r-1, c-1) + p(r-1, c)
;; p(r, 1) = 1, p(r, r) = 1
(define (pt r c)
  (cond ((or (= c 1) (= r c)) 1)
        ((or (< c 0) (< r 0) (> c r)) 0)
        (else (+ (pt (- r 1) (- c 1))
                 (pt (- r 1) c)))))

* > 또는 < 를 제대로 rendering 못하는 버그 있음. 위 pascal's triangle 코드의 6번째 줄에서 < 가 <; 로 출력되고 있음.

저작자 표시 비영리
신고

(define (inc x) (+ x 1))

(define (double f)
  (lambda (x) (f (f x))))

;; 맞바꿈 계산법으로 확인하기
(((double (double double)) inc) 5)

(((double (lambda (x) (double (double x)))) inc) 5)

(((lambda (y) ((lambda (x) (double (double x))) ((lambda (z) (double (double z))) y))) inc) 5)

(((lambda (x) (double (double x))) ((lambda (z) (double (double z))) inc)) 5)

(((lambda (x) (double (double x))) (double (double inc))) 5)

((double (double (double (double inc)))) 5)

((double (double (double (lambda (x) (inc (inc x)))))) 5)

((double (double (lambda (a)
                   ((lambda (x) (inc (inc x)))
                    ((lambda (x) (inc (inc x))) a))))) 5)

((double (lambda (b)
           ((lambda (a)
              ((lambda (x) (inc (inc x)))
               ((lambda (x) (inc (inc x))) a)))
            ((lambda (a)
               ((lambda (x) (inc (inc x)))
                ((lambda (x) (inc (inc x))) a))) b)))) 5)
((lambda (c)
   ((lambda (b)
      ((lambda (a)
         ((lambda (x) (inc (inc x)))
          ((lambda (x) (inc (inc x))) a)))
       ((lambda (a)
          ((lambda (x) (inc (inc x)))
           ((lambda (x) (inc (inc x))) a))) b)))
    ((lambda (b)
      ((lambda (a)
         ((lambda (x) (inc (inc x)))
          ((lambda (x) (inc (inc x))) a)))
       ((lambda (a)
          ((lambda (x) (inc (inc x)))
           ((lambda (x) (inc (inc x))) a))) b))) c))) 5)

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (b)
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a)))
     ((lambda (a)
        ((lambda (x) (inc (inc x)))
         ((lambda (x) (inc (inc x))) a))) b))) 5))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (a)
    ((lambda (x) (inc (inc x)))
     ((lambda (x) (inc (inc x))) a)))
  ((lambda (a)
     ((lambda (x) (inc (inc x)))
      ((lambda (x) (inc (inc x))) a))) 5)))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (a)
    ((lambda (x) (inc (inc x)))
     ((lambda (x) (inc (inc x))) a)))
  ((lambda (x) (inc (inc x)))
   ((lambda (x) (inc (inc x))) 5))))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (a)
    ((lambda (x) (inc (inc x)))
     ((lambda (x) (inc (inc x))) a)))
  ((lambda (x) (inc (inc x)))
   (inc (inc 5)))))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (a)
     ((lambda (x) (inc (inc x)))
      ((lambda (x) (inc (inc x))) a)))
   (inc (inc (inc (inc 5))))))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (x) (inc (inc x)))
      ((lambda (x) (inc (inc x))) (inc (inc (inc (inc 5)))))))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 ((lambda (x) (inc (inc x)))
      (inc (inc (inc (inc (inc (inc 5))))))))

((lambda (b)
   ((lambda (a)
      ((lambda (x) (inc (inc x)))
       ((lambda (x) (inc (inc x))) a)))
    ((lambda (a)
       ((lambda (x) (inc (inc x)))
        ((lambda (x) (inc (inc x))) a))) b)))
 (inc (inc (inc (inc (inc (inc (inc (inc 5)))))))))

((lambda (a)
   ((lambda (x) (inc (inc x)))
    ((lambda (x) (inc (inc x))) a)))
 ((lambda (a)
    ((lambda (x) (inc (inc x)))
     ((lambda (x) (inc (inc x))) a))) (inc (inc (inc (inc (inc (inc (inc (inc 5))))))))))

((lambda (a)
   ((lambda (x) (inc (inc x)))
    ((lambda (x) (inc (inc x))) a)))
 ((lambda (x) (inc (inc x)))
   ((lambda (x) (inc (inc x))) (inc (inc (inc (inc (inc (inc (inc (inc 5))))))))))) 

((lambda (a)
   ((lambda (x) (inc (inc x)))
    ((lambda (x) (inc (inc x))) a)))
 ((lambda (x) (inc (inc x)))
   (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc 5)))))))))))) 

((lambda (a)
   ((lambda (x) (inc (inc x)))
    ((lambda (x) (inc (inc x))) a)))
 (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc 5)))))))))))))

((lambda (x) (inc (inc x)))
 ((lambda (x) (inc (inc x))) (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc 5))))))))))))))

(inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc (inc 5))))))))))))))))

결론: double에 double을 적용하면 제곱으로 늘어남. (((double (double (double double))) inc) 0) ;; => 256

저작자 표시 비영리
신고

티스토리 툴바