As I’m currently studying for a BSc in Mathematics with the Open University and I am trying to teach myself Common Lisp, I decided to try and create a function that outputs the Fibonacci nth element. Here it is, I think is a nice example to illustrate how recursion works in Lisp.
(defun fibonacci (n)
(cond
((> n 1) (+ (fibonacci (- n 1)) (fibonacci (- n 2))))
((= n 1) 1)
((= n 0) 0)
(T (print "invalid number"))))
Fibonacci sequence in Common Lisp – the power of Lisp recursion
Android programming tips ebook cooming soon - fill in this quick form to be notified when the ebook comes out
1 Comment »
RSS feed for comments on this post. TrackBack URL
Similar in dc(1), just for fun. It too uses recursion, and slows as N increases. dc -e ‘[q]sq[d0=qd1=qd1-lfxr2-lfx+]sf?lfxp’ <<<13
Comment by Ralph Corderoy — April 16, 2011 @ 12:06 pm