sicp 习题 1.34 ~ 1.37

1.34
(define (square n)
  (* n n))

(define (f g)
  (g 2))

(f square)

(f (lambda (z) (* z (+ z 1))))

(f f)


1.35
(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 cos 1.0)

(fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)


1.36
(define tolerance 0.00001)

(define (average a b)
  (/ (+ a b) 2))

(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (newline)
      (display next)
      (if (close-enough? guess next)
          next
          (try next))
      ))
    (try first-guess))

(fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0) ;;34
(fixed-point (lambda (x) (average x (/ (log 1000) (log x)))) 2.0) ;;9


1.37
(define (cont-frac n d i k)
  (if (= k 1)
      (/ (n 1) (d 1))
      (/ (n i) (+ (d i) (cont-frac n d (+ i 1) (- k 1))))))

(define (cont-frac-r n d k)
  (define (frac i)
    (if (= i (+ k 1))
        0
        (/ (n i) (+ (d i) (frac (+ i 1))))))
  (frac 1))

(define (cont-frac-i n d k)
  (define (frac i v)
    (if (= i 0)
        v
        (frac (- i 1) (/ (n i) (+ (d i) v)))))
  (frac k 0))

(cont-frac-r (lambda (i) 1.0)
           (lambda (i) 1.0)
           11)

(cont-frac-i (lambda (i) 1.0)
           (lambda (i) 1.0)
           11)

(cont-frac (lambda (i) 1.0)
           (lambda (i) 1.0)
           1
           11)

(cont-frac-i (lambda (i) 1.0)
           (lambda (i) 1.0)
           11)

你可能感兴趣的:(F#)