SCIPの1.11を解いてみた。
めずらしく英語のページを読む気になった。
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.
>>(define (f n) (cond ((= n 1) 1) ((= n 2) 2) ((= n 3) 3) ((> n 3) (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))) =>f >>(f 4) =>10 >>(f 5) =>22
たぶんこれであってると思う。
match-lambda版は
>>(use util.match) >>(define f (match-lambda (1 1) (2 2) (3 3) (n (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))) =>f >>(f 4) =>10