Have you considered the similarity between loop-recur and sequence operations?
IE, tail-recursion turns the call stack into a sequence of states. The nice trick you can play in clojure is to reify this sequence as a lazy sequence. (take 5 ((fn this-fn [last] (cons (inc last) (lazy-seq (this-fn (inc last))))) 0)) It still looks recursive, but the values of the stack are spread across some java objects instead of disappearing (due to TCO if we had it or the explicit tail-recur that we do have). On Tue, Jun 3, 2014 at 5:00 PM, Mike Fikes <mikefi...@me.com> wrote: > I'm trying to “internalize” loop/recur as being a functional construct. > > I'm sure I'm not alone in this: I initially struggled with it, with my > view unduely influenced by its implementation, especially when multiple > loop heads are present. (It could be viewed as vulgar but necessary > imperative optimization, playing tricks with the instruction pointer). > > Here is a line of reasoning that I'm interested in comments on: > > Take this example: > > (defn vrange [n] > (loop [i 0 v []] > (if (< i n) > (recur (inc i) (conj v i)) > v))) > > > This can be rewritten applying a named inline function that closes over n: > > (defn vrange2 [n] > ((fn loop-recur [i v] > (if (< i n) > (loop-recur (inc i) (conj v i)) > v)) 0 [])) > > > Then, this could be further transformed: > > (defn loop-recur [i v n] > (if (< i n) > (loop-recur (inc i) (conj v i)) > v)) > > (defn vrange3 [n] > loop-recur 0 [] n) > > > which is clearly functional. > > I'm cool with this view. It with it, you see recur not so much as as not > “jumping” to the nearest loop head, but instead as returning a value that > results from ”calling” a function, where that function is kind-of defined > by the loop head. > > I was wondering? Is the above transformation always possible, even given > multiple loop heads? Is it always this simple, or are there corner cases > that this naïve view misses? > > -- > 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.