Cogitas Blog:
Google Android, machine learning,
natural language processing
and Java programming.


For Devs Info
Tips and code for app, website and server developers.

Fibonacci sequence in Common Lisp – the power of Lisp recursion

Filed under: programming — Tags: , — April 15, 2011

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"))))

Android programming tips ebook cooming soon - fill in this quick form to be notified when the ebook comes out

1 Comment »

  1. 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

RSS feed for comments on this post. TrackBack URL

Leave a comment