Re: noob question about error with tail recursion
(seq? ()) is true. You want (seq all), not (seq? all). There may be other problems, but that one jumps out at me. On Nov 30, 1:53 pm, coco wrote: > yep..that is an error but (recur (rest rst) I think wouln't work...maybe > something like > > (defn packing [lista] > (loop [[fst snd :as all] lista mem [] tmp '(fst)] > > (print "all is " all "\n\n") ;;something is wrong...all always is > a empty list > > (if (seq? all) > (if (= fst snd) > (recur (rest all) mem (cons snd tmp)) > (recur (rest all) (conj mem tmp) (list snd))) > (seq mem > > something is still wrong..:( -- 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
Re: noob question about error with tail recursion
yep..that is an error but (recur (rest rst) I think wouln't work...maybe something like (defn packing [lista] (loop [[fst snd :as all] listamem []tmp '(fst)] (print "all is "all "\n\n") ;;something is wrong...all always is a empty list (if (seq? all) (if (= fst snd) (recur (rest all) mem (cons snd tmp)) (recur (rest all) (conj mem tmp) (list snd))) (seq mem something is still wrong..:( -- 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
Re: noob question about error with tail recursion
yep..that is an error but (recur (rest rst) I think wouln't work...maybe something like (defn packing [lista] (loop [[fst snd :as all] listamem []tmp '(fst)] (print "all is "all "\n\n") ;;something is wrong...all always is a empty list (if (seq? all) (if (= fst snd) (recur (rest all) mem (cons snd tmp)) (recur (rest all) (conj mem tmp) (list snd))) (seq mem something is still wrong..:( 2011/11/30 Chris Perkins > (recur (rest lista) ...) > here: ^^^ > > lista is always the same thing. You probably meant (recur (rest rst) ...). > > - Chris > > -- > 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 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
Re: noob question about error with tail recursion
(recur (rest lista) ...) here: ^^^ lista is always the same thing. You probably meant (recur (rest rst) ...). - Chris -- 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
Re: noob question about error with tail recursion
now I'm in a similar trouble...I'm trying resolve the problem 31 from 4clojure ...It says: Write a function which packs consecutive duplicates into sub-lists. [image: test not run] (= (__ [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3))) I know than I can use identity and others clojure functions but I wanna resolve this using loop too my idea is it: I've a mem..this is the final list than I will return and a temporary list... first I compare my first and second item and if these are the same I include it in my temp list if they are different then I include my temp list inside mem (a better name would be final_list) and clear my temp putting the second item in it I iterate until than my list become empty the code could be this: (defn packing [lista] (loop [[fst snd & rst] listamem []tmp '(fst)] (if (seq? lista) (if (= fst snd) (recur (rest lista) mem (cons snd tmp)) (recur (rest lista) (conj mem tmp) (list snd))) (seq mem if I run this I get a java.lang.OutOfMemoryError: Java heap space (NO_SOURCE_FILE:0) It is tail recursive, I'm not sure if I can use 2 distinct recursions inside a loop but I think than must be possible..what am I doing wrong??... thanks so much -- 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
Re: noob question about error with tail recursion
coco writes: > thanks for the answer, well, I rewrote the function lik this: > > (defn recursive-reverse [coll] > (loop [col coll mem '()] > (if (empty? col) > mem > (recur (rest col) (cons (first col) mem)) ))) > > I've a little logic problem but I fix it One minor remark: Instead of (empty? col) one usually uses (seq col), which returns nil if col is empty and non-nil otherwise. (So you'd have to swap the branches of the if.) (defn recursive-reverse [coll] (loop [col coll mem '()] (if (seq col) (recur (rest col) (cons (first col) mem)) mem))) Bye, Tassilo -- (What the world needs (I think) is not (a Lisp (with fewer parentheses)) but (an English (with more.))) Brian Hayes, http://tinyurl.com/3y9l2kf -- 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
Re: noob question about error with tail recursion
On Tue, Nov 22, 2011 at 9:22 PM, coco wrote: > thanks for the answer, well, I rewrote the function lik this: > > (defn recursive-reverse [coll] > (loop [col coll mem '()] > (if (empty? col) > mem > (recur (rest col) (cons (first col) mem)) ))) > > I've a little logic problem but I fix it Looks like you're on the right path - congrats! -- Sean A Corfield -- (904) 302-SEAN An Architect's View -- http://corfield.org/ World Singles, LLC. -- http://worldsingles.com/ "Perfection is the enemy of the good." -- Gustave Flaubert, French realist novelist (1821-1880) -- 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
Re: noob question about error with tail recursion
thanks for the answer, well, I rewrote the function lik this: (defn recursive-reverse [coll] (loop [col coll mem '()] (if (empty? col) mem (recur (rest col) (cons (first col) mem)) ))) I've a little logic problem but I fix it thanks On Nov 23, 12:54 am, Sean Corfield wrote: > On Tue, Nov 22, 2011 at 8:49 PM, coco wrote: > > ((cons (last col) mem) > > (recur (butlast col) mem) > ... > > ((cons (last col) mem) > > (recur col mem) > > In both of these, you have a function call with (cons (last col) mem) > as the function... > > You're going to need (recur something something-else) as the else > expression for recur to be in the tail position. > > HTH, > -- > Sean A Corfield -- (904) 302-SEAN > An Architect's View --http://corfield.org/ > World Singles, LLC. --http://worldsingles.com/ > > "Perfection is the enemy of the good." > -- Gustave Flaubert, French realist novelist (1821-1880) -- 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
Re: noob question about error with tail recursion
On Tue, Nov 22, 2011 at 8:49 PM, coco wrote: > ((cons (last col) mem) > (recur (butlast col) mem) ... > ((cons (last col) mem) > (recur col mem) In both of these, you have a function call with (cons (last col) mem) as the function... You're going to need (recur something something-else) as the else expression for recur to be in the tail position. HTH, -- Sean A Corfield -- (904) 302-SEAN An Architect's View -- http://corfield.org/ World Singles, LLC. -- http://worldsingles.com/ "Perfection is the enemy of the good." -- Gustave Flaubert, French realist novelist (1821-1880) -- 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
noob question about error with tail recursion
Hi..I've a little problem using recur for recursive calling...I'm trying do the clojure koans and the recursive reverse function [1 2 3 4] -> '(4 3 2 1) first I try this code: (defn recursive-reverse [coll] (loop [col coll mem '()] (if (empty? coll) mem ((cons (last col) mem) (recur (butlast col) mem) this didn't work and I thought the problem was (butlast col) and rewrite the code to this (defn recursive-reverse [coll] (loop [col (butlast coll) mem (list (last coll))] (if (empty? coll) mem ((cons (last col) mem) (recur col mem) why isn't a tail recur?...my recursion is de latest operation in the functin thanks so much I hope learn a lot about functional programming -- 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