Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created April 28, 2011 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubysolo/947410 to your computer and use it in GitHub Desktop.
Save rubysolo/947410 to your computer and use it in GitHub Desktop.
; return sequence of first n fibonacci numbers:
(defn fibs [n]
(case n
0 '()
1 '(1)
2 '(1 1)
(let [
x (- n 1)
y (- n 2)
z (- n 3)
s (fibs x)
]
(concat s [(+ (nth s y) (nth s z))])
)
)
)
; repl output:
;user=> user=> (= (fibs 3) '(1 1 2))
;true
;user=> (= (fibs 6) '(1 1 2 3 5 8))
;true
;user=> (= (fibs 8) '(1 1 2 3 5 8 13 21))
;true
;---
; 4clojure error:
; (used above function definition with 'fn' rather than 'defn')
; java.security.PrivilegedActionException: java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to clojure.lang.MapEntry (NO_SOURCE_FILE:0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment