Skip to content

Instantly share code, notes, and snippets.

@bostonou
Created March 31, 2015 03:55
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 bostonou/966eca107b1f970db3d8 to your computer and use it in GitHub Desktop.
Save bostonou/966eca107b1f970db3d8 to your computer and use it in GitHub Desktop.
lazy cat fibonacci
;;So let’s work out the first couple of simple cases:
(take 1 head-fibo)
(take 1 (lazy-cat [0N 1N] (map + head-fibo (rest head-fibo))))
(take 1 (concat (lazy-seq [0N 1N])
(lazy-seq (map + head-fibo (rest head-fibo)))))
;;since we’re taking 1, the 0N of the first lazy-seq suffices and
;;we don’t need to further evaluate
;; => (0N)
;;take 2 is similar, in that the first lazy-seq suffices by providing two values
;; => (0N 1N)
;;So now, to a more interesting case
(take 3 head-fibo)
(take 3 (lazy-cat [0N 1N] (map + head-fibo (read head-fibo))))
(take 3 (concat (lazy-seq [0N 1N])
(lazy-seq (map + head-fibo (rest head-fibo)))))
;;now, the first lazy-seq only satisfies 2 out of the 3 values needed.
;;so we process the next lazy-seq
(take 3 (concat (lazy-seq [0N 1N])
(lazy-seq (map +
(lazy-cat [0N 1N] (map + head-fibo (resd head-fibo)))
(rest (lazy-cat [0N 1N] (map + head-fibo (resd head-fibo))))))))
(take 3 (concat (lazy-seq [0N 1N])
(lazy-seq (map +
(concat (lazy-seq [0N 1N]) (lazy-seq (map + head-fibo (rest head-fibo))))
(rest (concat (lazy-seq [0N 1N]) (lazy-seq (map + head-fibo (rest head-fibo)))))))))
;;since we need one more value, we map over the first value of each lazy-seq, namely 0N and 1N, giving:
(take 3 (concat (lazy-seq [0N 1N])
(lazy-seq (map +
0N
1N))))
;; => (0N 1N 1N)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment