Re: A generalization of iterate

2015-09-28 Thread Yoshinori Kohyama
Hi nchurch and all. Your idea to generalize `iterate` looks very cool for me. I wrote another implementation not to consume stack. (defn generate' [f h & r] (cons h (lazy-seq (apply generate' f (reverse (cons (apply f h r) (reverse r))) will work, if it is o.k. that we ca

Re: A generalization of iterate

2015-09-23 Thread nchurch
A, right. Silly me. This is something I have trouble remembering. For some reason when you use regular concat, the error message is helpful (can't recur from non-tail position); with lazy-cat it shows a mismatch of argument numbers. On Wednesday, September 23, 2015 at 2:10:47 PM UTC-7, Max C

Re: A generalization of iterate

2015-09-23 Thread Max Countryman
Your problem with recur is because you can only recur from the tail position. > On Sep 23, 2015, at 12:28, nchurch 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; an

Re: A generalization of iterate

2015-09-23 Thread nchurch
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,

Re: A generalization of iterate

2015-09-22 Thread Max Countryman
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: (ta

A generalization of iterate

2015-09-22 Thread nchurch
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 a