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] lista mem [] 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 [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