SICP练习

练习1.8

(define (good-enough? old new)
    (and (< (/ old new) 1.001) (> (/ old new) 0.999)))
(define (improve  guess x)
    (/ (+ (/ x (* guess guess)) (* 2 guess)) 3))
(define (cube-root guess x)
    (if (good-enough? guess (improve guess x))
        guess
        (cube-root (improve guess x) x)))
(define (cuberoot x) (cube-root 1.0 x))

 练习1.30

(define (sum a b term next)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))

测试

> (sum 1 5 (lambda (x) x) (lambda (x) (+ x 1)))
15
> (sum 1.0 5.0 (lambda (x) (/ 1 x)) (lambda (x) (+ x 1)))
2.283333333333333

注意

(lambda (x) (x))    错误写法
(lambda (x) x)       正确写法

练习1.34

理论上,(f f) 等价于 (f 2)等价于(2 2)  应该会报过程未定义的错

SICP练习_第1张图片

可见二者的报错是相同的

你可能感兴趣的:(SICP)