昨日は Python でやったので、今日は Scheme で。
(define tribonacci
(lambda (n)
(let loop ((m n) (l '(0 0 1)))
(if (= m 0)
'()
(cons (car l) (loop (- m 1) (append (cdr l) (list (apply + l)))))))))
(print (tribonacci 20))
今回は名前つきletで再帰。
実行:
^o^ > gosh tribonacci.scm (0 0 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513)