public
Created

  • Download Gist
fibs.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
; 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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.