Home SICP 연습문제 1.11
Post
Cancel

SICP 연습문제 1.11

n < 3 일 때 f(n) = n이고, n >= 3 일 때 f(n) = f(n-1) + 2f(n-2) + 3f(n-3)으로 정의한 함수가 있다. f의 프로시저를 되도는 프로세스를 만들고 반복 프로세스를 만들어 내는 프로시저도 만들어라.

Recursive Process

1
2
3
4
5
6
(define (f n)
(if (&lt; n 3)
n
(+ (f (- n 1)) (* 2 (f(- n 2))) (* 3 (f(- n 3))))))

Iterative Process

1
2
3
4
5
6
7
8
9
10
(define (f2 n)
(define (f-iter first second third counter)
(if (= counter 3)
(+ third (* 2 second) (* 3 first))
(f-iter second third (+ third (* 2 second) (* 3 first)) (- counter 1))))
(if (&lt; n 3)
n
(f-iter 0 1 2 n)))

This post is licensed under CC BY 4.0 by the author.

SICP 연습문제 1.10

Common Lisp, Emacs, slime 셋팅하기