sicp每日一题[1.34-1.35]

Exercise 1.34

Suppose we define the procedure
(define (f g) (g 2))
Then we have

(f square)
4
(f (lambda (z) (* z (+ z 1))))
6

What happens if we(perversely) ask the interpreter to evaluate the combination ( f f ) (f f) (ff)? Explain.

answer: 执行之后报错了,因为 f f f需要一个程序来作为参数,而我们传了个数字2给它
在这里插入图片描述

Exercise 1.35

Show that the golden ratio ϕ ϕ ϕ (Section 1.2.2) is a fixed point of the transformation x = 1 + 1 x x = 1 + \frac{1}{x} x=1+x1, and use this fact to compute ϕ ϕ ϕ by means of the fixed-point procedure.

answer: 证明 x = 1 + 1 x x = 1 + \frac{1}{x} x=1+x1的解是黄金分割比例就是简单的一元二次方程求解,这里就不说明了,利用 f i x e d − p o i n t fixed-point fixedpoint程序求解也很简单,直接把书上1.3.3部分的程序拿来用就行

(define tolerance 0.001)

(define (close-enough? x y tolerance)
  (< (abs (- x y)) tolerance))

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

; calculate the golden ratio φ by solve the equation: x = 1 + 1/x
(fixed-point (lambda (x) (+ 1 (/ 1 x)))
             1.0)

; result: 1.6181818181818182
  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值