This commit is contained in:
NaiJi ✨ 2020-07-25 02:36:24 +03:00
parent 8d27dd2d10
commit 0faa62c745
1 changed files with 27 additions and 0 deletions

27
sicp-ex-1.11.rkt Normal file
View File

@ -0,0 +1,27 @@
; f(n) = n, when n < 3
; f(n) = f(n - 1) + f(n - 2) + f(n - 3), when n >= 3
(define fn 15)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; recursive
(define (f-rec n)
(if (< n 3)
n
(+ (+ (f-rec (- n 1))
(f-rec (- n 2)))
(f-rec (- n 3)))))
(f-rec fn)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; iterative
(define (f-iter n)
(f-iter-step 2 1 0 n))
(define (f-iter-step a b c count)
(if (< count 3)
a
(f-iter-step (+ a b c) a b (- count 1))))
(f-iter fn)