I haven't really used CPS myself, but I think you should be able to use the trampoline function to clean up your code.
http://clojuredocs.org/clojure_core/clojure.core/trampoline Jonathan On 15 July 2014 09:13, Mark P <pierh...@gmail.com> wrote: > I'm very new to continuation passing style (CPS), and as part of the > learning process I've done a CPS version of a "flatten" function. Ie, it > does the same thing as the standard clojure "flatten", but is implemented > using a recursive local CPS helper function. I'm interested in comments / > critiques on what I've done. > > Here it is... > > (defn my-flatten > [xs] > (letfn [(my-flatten-cps [xs k] > (if (nil? xs) > (k '()) > (let [x (first xs), ys (next xs)] > (if (sequential? x) > (recur ys (fn [v] (my-flatten-cps (seq x) (fn [w] (k > (concat w v)))))) > (recur ys (fn [v] (k (conj v x))))))))] > (my-flatten-cps (seq xs) identity))) > > I'm relatively inexperienced with clojure, so please feel free to suggest > improvements to my clojure code. > > But what I'm most interested in is understanding CPS better and about how > it interacts with Clojure and with "recur". As you can see, my-flatten-cps > uses recur nicely to traverse at a breadth level. But as we sink down into > depth, I've done an actual non-recur call to my-flatten-cps. I presume I > can't do a recur here instead (because it would attempt to jump to the most > immediate fn??) - is this correct? > > Is there any way around this? (As I write this, the word "trampoline" > comes to mind - some videos I've watched speak of this - but not sure how > this would work and what efficiency trade-offs would be involved.) > > The other things is... is it so bad that it is not fully using recur - > maybe using a bit of stack is okay?? > > Thanks, > > Mark. > > -- > 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.