sicp习题试解 (1.11)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

; ======================================================================

;

; Structure and Interpretation of Computer Programs

; (trial answer to excercises)

;

; 计算机程序的构造和解释(习题试解)

;

; created: code17 02/25/05

; modified:

; (保持内容完整不变前提下,可以任意转载)

; ======================================================================

;; SICP No.1.11

;; recursive process

(define (fr n)

(if (< n 3)

n

(+ (fr (- n 1))

(* 2 (fr (- n 2)))

(* 3 (fr (- n 3))))))

;; iterative process

(define (fi n)

;; local iterative procedure

(define (f-iter x y z i)

(if (= i n)

x

(f-iter (+ x (* y 2) (* z 3)) x y (+ i 1))))

;; main procedure

(if (< n 3) n (f-iter 2 1 0 2)))

;; Test-it:

;; > (fr 2)

;; 2

;; > (fi 2)

;; 2

;; > (fr 5)

;; 25

;; > (fi 5)

;; 25

;; > (fr 23)

;; 144191392

;; > (fi 23)

;; 144191392

;; >

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航