Created
October 27, 2009 13:26
-
-
Save jcromartie/219554 to your computer and use it in GitHub Desktop.
annotated for learning purposes
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
(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