Last active
August 29, 2015 14:04
-
-
Save ShigekiKarita/30859cd736f5622b11e5 to your computer and use it in GitHub Desktop.
How to get 10th Fibonacci number in Common Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; recur | |
(defun fib (i &optional (a 1) (b 0)) | |
(if (= i 1) | |
a | |
(fib (1- i) (+ a b) a))) | |
(fib 10) | |
;; do macro | |
(do ((i 1 (1+ i)) | |
(a 0 b) | |
(b 1 (+ b a))) | |
((< 10 i) a)) | |
;; loop macro | |
(loop for i from 1 to 10 | |
and a = 0 then b | |
and b = 1 then (+ b a) | |
finally (return a)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment