Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created October 27, 2009 13:26
Show Gist options
  • Save jcromartie/219554 to your computer and use it in GitHub Desktop.
Save jcromartie/219554 to your computer and use it in GitHub Desktop.
annotated for learning purposes
(defn fibs
; this is the doc string
"Returns a lazy sequence of all the Fibonacci numbers."
; this is the argument list
[]
; map takes a function and a seq and returns a lazy seq where the function is
; applied to each element of the input seq... since it's lazy you can do this
; with infinite sequences
(map
; first is the function argument to map, and it just returns the first element of a collection
first
; the seq argument to map is this iterate form
; iterate takes a function f and a value, and yields a lazy seq of f(x), f(f(x)), f(f(f(x))), etc.
(iterate
; fn is (one of) clojure's lambda form, and this is the function argument to iterate
(fn
; the outer [] denotes the argument list to this fn
; the inner [a b] is a destructuring form which means "bind the first and second
; elements of a vector argument to a and b"
[[a b]]
; make a vector with b and the sum of a and b ([] is the vector literal)
[b (+ a b)])
; this is the intial value for iterate, so we get (fn [0 1]) first, then (fn [1 (+ 0 1)]) then
; (fn [(+ 0 1) (+ 1 (+ 0 1))]) successively
[0 1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment