Re: How to convert general recursion to loop .. recur syntax

2011-09-16 Thread Herwig Hochleitner
Am Donnerstag, 15. September 2011 schrieb Ken Wesson : > On Thu, Sep 15, 2011 at 8:56 AM, Herwig Hochleitner > > wrote: > > Consider > > > > (defn find-in-tree > > ([tree pred?] > >(concat > > (filter pred? tree) > > (mapcat find-in-tree (filter sequential? tree) (repeat pred?)

Re: How to convert general recursion to loop .. recur syntax

2011-09-15 Thread octopusgrabbus
Thanks for clarifying the stack space issue. I got confused with the original implementation, and was unsure it would run with a large sequence. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups

Re: How to convert general recursion to loop .. recur syntax

2011-09-15 Thread Ken Wesson
On Thu, Sep 15, 2011 at 8:56 AM, Herwig Hochleitner wrote: > Consider > > (defn find-in-tree >  ([tree pred?] >    (concat >      (filter pred? tree) >      (mapcat find-in-tree (filter sequential? tree) (repeat pred?) > > which of course is much simpler written as > > (defn find-in-tree >  ([

Re: How to convert general recursion to loop .. recur syntax

2011-09-15 Thread Chouser
On Thu, Sep 15, 2011 at 8:56 AM, Herwig Hochleitner wrote: > Hi, > > as you might know, the original version actually does run in fixed > stack space, thanks to lazy sequences. You are right! Without testing it I had thought that the way recursion was used would cause skl to overflow the stack i

Re: How to convert general recursion to loop .. recur syntax

2011-09-15 Thread Herwig Hochleitner
Hi, as you might know, the original version actually does run in fixed stack space, thanks to lazy sequences. (defn skl [tree] (map skl (filter seq? tree))) loop .. recur is the moral equivalent of a while(true) loop in imperative style. i.e. a slightly disguised (and somewhat more structure

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Chouser
On Wed, Sep 14, 2011 at 3:58 PM, octopusgrabbus wrote: > Alan: > > I may have misunderstood what I've read both in books, blogs, and the > Clojure site, but it seems that writing recursive functions in the loop .. > recur style is the preferred style. I also remember most of the texts > currently

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Laurent PETIT
Hi, 2011/9/14 Alan Malloy > You can prefer anything you want, but (a) to say that Clojure prefers > loop/recur is nonsense, and (b) you can't make an incorrect algorithm > work just by preferring it. Jeff is correct that your algorithm > requires space for each level of the tree, and so cannot b

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Laurent PETIT
2011/9/14 Meikel Brandmeyer > Hi, > > Am 14.09.2011 um 16:54 schrieb octopusgrabbus: > > > (defn skl > > [tree] > > (map skl (filter seq? tree))) > > > > Is that what you want? > > (defn skl > [tree] > (loop [output [] > tree (seq tree)] >(if tree > (let [fst (first tree)

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread octopusgrabbus
Alan: I may have misunderstood what I've read both in books, blogs, and the Clojure site, but it seems that writing recursive functions in the loop .. recur style is the preferred style. I also remember most of the texts currently out on Clojure say use the higher level sequence functions rath

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread octopusgrabbus
Thanks. I'll have a look. -- 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

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Alan Malloy
You can prefer anything you want, but (a) to say that Clojure prefers loop/recur is nonsense, and (b) you can't make an incorrect algorithm work just by preferring it. Jeff is correct that your algorithm requires space for each level of the tree, and so cannot be converted into a constant-space alg

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Meikel Brandmeyer
Hi, Am 14.09.2011 um 16:54 schrieb octopusgrabbus: > (defn skl > [tree] > (map skl (filter seq? tree))) > Is that what you want? (defn skl [tree] (loop [output [] tree (seq tree)] (if tree (let [fst (first tree)] (if (seq? fst) (recur (conj output

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread octopusgrabbus
Jeff: loop .. recur syntax is Clojure's preferred method of recursion. This is a routine to return the skeleton of a sequence, not its values. cmn -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegr

Re: How to convert general recursion to loop .. recur syntax

2011-09-14 Thread Jeff Palmucci
In general, you can't convert recursion into loops. Recursion has stack frames, loops don't. I can't really tell what you are trying to do here because your example just walks the interior nodes of the expression tree, doing nothing. Can you clarify with a more complete example? -- You received