sicp 学习 1.2

       Exercise 1.11. A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n -
3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that

computes f by means of an iterative process.

iterative:

16(define (f n)
17  (define (f-iter product1 product2 product3 counter)
18    (if (= 0 counter)
19        product3
20        (f-iter (+ product1 (* 2 product2) (* 3 product3))
21                product1
22                product2
23                (- counter 1))))
24  (f-iter 2 1 0 n))
 

recuisive:

77(define (f n)
78  (cond ((= n 0) 0)
79        ((= n 1) 1)
80        ((= n 2) 2)
81        (else (+ (* 1 (f (- n 1)))
82                 (* 2 (f (- n 2)))
83                 (* 3 (f (- n 3)))))))

Exercise 1.12. The following pattern of numbers is called Pascal's triangle.
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two
numbers above it.35 Write a procedure that computes elements of Pascal's triangle by means of a recursive
process.


95(define (pascal x y)
 96  (cond ((= x 1) 1)
 97        ((= x y) 1)
 98        (else (+ (pascal (- x 1) (- y 1))
 99                 (pascal x (- y 1))))))

x ,y start from 1 respectively.




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值