In Clojure, how can I write my own recursive function to work with lazy lists?
I was just trying to demonstrate to someone laziness, and I wrote a simple recursive function to process the list.
I assumed it would be fine on an endless list. But all of a sudden I got an internal "too much recursion" error.
AND? I always write code that does this kind of thing. What is the problem?
But of course, I usually use built- map
in type functions as the basis for other list processing functions. This time I tried to write my own recursive traversal. And of course this may not work.
Here's what I wrote.
(defn q [xs]
(if (empty? xs) ()
(cons (* (first xs) (first xs)) (q (rest xs)) )))
(take 10 (q (cycle '(1 2 3 4))))
So how do I actually write my own traversal functions that can handle lazy data structures in Clojure? Is there any equivalent to "profitability"?
+3
source to share