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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.