Thanks Meikel- Let me see if I understand what's going on here... Usually, calling "left-total" recursively from a non-tail call position, as you do in this example, would abuse the call stack. However, since the call is happening from within a lazy-seq, does this mean the number of items on the call stack will remain unhindered, despite the non-tail-call recursion?
Experimentally, this version seems to perform as well as any of my solutions (under 2 secs for 1 mil items) This suggests it is, indeed, not causing any abuse of the call stack. This is definitely cleaner and more flexible than any other solution suggested so far, I think. On Dec 28, 9:11 am, Meikel Brandmeyer <m...@kotka.de> wrote: > Hi, > > Am 28.12.2009 um 02:36 schrieb Conrad: > > > => (left-total [3 5 10 1 2 7]) > > ([3 0] [5 3] [10 8] [1 18] [2 19] [7 21]) > > If in doubt, use lazy-seq. > > (defn left-total > [accum-fn accum s] > (lazy-seq > (when-let [[f & r] (seq s)] > (cons [f accum] (left-total accum-fn (accum-fn accum f) r))))) > > user=> (left-total + 0 [3 5 10 1 2 7]) > ([3 0] [5 3] [10 8] [1 18] [2 19] [7 21]) > > Since you said, that + is more complicated in your specific case, you might > want this more general form. Otherwise the above can of course be simplified… > > Sincerely > Meikel -- 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