Re: Computing fibonacci numbers (rank newbie)




Jeff:

I forgot to edit FIB-RECUR to print the Fibonacci numbers in ascending
order, if you prefer things that way. The (VALUES) procedure at the
end of the second COND clause ensures that there is no return value
(i.e., no NIL at the end).

See chapters 7 and 22 of Practical Common Lisp by Peter Seibel for
additional iterative versions for computing Fibonacci numbers using DO
and LOOP.


(defun fib-recur (x &optional (y 0))
(cond ((zerop (1+ x)))
(t (print (fibber y))
(fib-recur (1- x) (1+ y))
(values))))
FIB-RECUR


(defun fibber (x)
(if (or (= x 0) (= x 1))
1
(+ (fibber (- x 1))
(fibber (- x 2)))))
FIBBER


(fib-recur 8)
1
1
2
3
5
8
13
21
34



agthompson

.