I don't see why the cseq's have to be lazy, they are at the most 525
elements long.
shouldn't each sequence only be produced when it is reduced in max-key and
then discarded?
....
But it makes a difference:

(defn cseq [n]
  (if (= 1 n)
    [1]
    (lazy-seq (cons n (cseq (if (even? n)
                  (/ n 2)
                  (+ (* 3 n) 1 )))))))

(apply max-key count (map cseq (range 1 1000000)))

gives me results.

(def a (apply max-key count (map cseq (range 1 1000000))))
gives a heap error.

Thanks for your answer.

2011/1/17 Mark Engelberg <mark.engelb...@gmail.com>

> Your cseq is not lazy, and some of the sequences can be quite long, so
> it wouldn't surprise me if that's the source of your problem.
>
> You can test if this is the problem by doing something like:
> (dorun (map cseq (range 1 1000000))) which removes the max-key from
> the computation entirely.
>
> You'll probably need to reformulate cseq with lazy lists.  Even then,
> you will likely find that this program will be too slow without
> further optimizations (e.g., memoization or dynamic programming).
>
> That's the nature of project Euler problems; the "obvious" way to
> solve the problem is often too slow and further cleverness is
> required.
>
> On Mon, Jan 17, 2011 at 7:53 AM, Andreas Liljeqvist <bon...@gmail.com>
> wrote:
> > Hi.
> >
> > I am using max-key to get the longest sequence from a couple of
> sequences.
> >
> > (defn cseq [n]
> >   (if (= 1 n)
> >     [1]
> >     (cons n (cseq (if (even? n)
> >             (/ n 2)
> >             (+ (* 3 n) 1 ))))))
> >
> > (apply max-key count (map cseq (range 1 1000000)))
> >
> > This gives me a heap error.
> >
> > To my understanding max-key should keep at most two sequences in memory.
> > I could certainly solve this by working around the problem, but I want to
> > understand where my logic fails.
> > Thankful for any help.
> >
> >
> > --
> > 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<clojure%2bunsubscr...@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<clojure%2bunsubscr...@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

Reply via email to