Your problem with recur is because you can only recur from the tail position. 

> On Sep 23, 2015, at 12:28, nchurch <nchubr...@gmail.com> wrote:
> 
> Yeah, it consumes stack just like the Clojurescript version of Iterate.  I'm 
> curious if anyone knows how to avoid this.  The Clojure version of Iterate is 
> Java; and for some reason 'recur' can't be used inside of lazy-cat (not 
> really documented, but apparently true). 
> 
>> On Tuesday, September 22, 2015 at 9:21:57 PM UTC-7, Max Countryman wrote:
>> I wonder if something like this is a little easier to read?
>> 
>> (defn generate
>>   ([f coll]
>>    (generate f coll (reverse coll)))
>>   ([f coll args]
>>    (let [next-val (apply f args)]
>>      (lazy-cat coll (generate f [next-val] (conj (butlast args) 
>> next-val))))))
>> 
>> Where your Fibonacci example becomes:
>> 
>> (take 100 (generate +’ [1 1]))
>> 
>> As for your other questions, I can’t be of much help. But isn’t there 
>> nontrivial memory overhead to consider with this approach?
>> 
>> 
>>> On Sep 22, 2015, at 18:19, nchurch <nchu...@gmail.com> wrote:
>>> 
>>> I was going through 4clojure (highly recommended!) and doing the Fibonacci 
>>> exercise.  It occurred to me that the iterate function could be generalized 
>>> in a reasonable way, so that the next value is generated by applying the 
>>> function to the last N items so far, where N is the number of initial 
>>> arguments beyond the function.  Thus:
>>> 
>>> (generate inc 1) 
>>> 
>>> works just like iterate, while
>>> 
>>> (generate + 1 1)
>>> 
>>> generates the Fibonacci sequence.
>>> 
>>> Here's the code:
>>> 
>>> (defn generate
>>>   [f & more]
>>>   (letfn [(recurse
>>>             [coll args]
>>>             (let [next-val (apply f args)]
>>>               (lazy-cat coll (recurse [next-val] (conj (butlast args) 
>>> next-val)))))]
>>>     (recurse more (reverse more))))
>>> 
>>> Code is also on Github.
>>> 
>>> I see this as part of a larger class of generalized sequence functions: for 
>>> instance, extra arguments in a function given to reduce could refer to N 
>>> arguments back in the sequence (might be useful, for instance, in smoothing 
>>> a sequence of values using an average).
>>> 
>>> Questions: is there an existing home for this kind of functionality?  And 
>>> could the above code be improved?  (I just used the definition of iterate 
>>> as a starting point.)
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clo...@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with 
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+u...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>> --- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to clojure+u...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> --- 
> You received this message because you are subscribed to the Google Groups 
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to