Hi,
I'm Marek Stępniowski, a Python developer trying to learn a new
language by night. I'm new to this group.

When solving problem 45 from Project Euler [1] I have tried to learn
how lazy-seq macro should be used and wrote the code below::

(defn pentagonal-numbers []
 (map #(* 1/2 % (dec (* 3 %))) (iterate inc 1)))

(defn hexagonal-numbers []
 (map #(* % (dec (* 2 %))) (iterate inc 1)))

(defn equal-values [seqs]
 "Given a list of ascending sequences, returns a lazy sequence containing
 only values that exist in all of the sequences."
 (if (empty? (first seqs))
   []
   (let [first-values (map first seqs)]
     (if (apply = first-values)
       (lazy-seq (cons (first first-values)
                       (equal-values (map rest seqs))))
       (let [min-value (apply min first-values)]
         (recur (map #(if (= (first %) min-value) (rest %) %) seqs)))))))

Unfortunately running::

(take 3 (equal-values [(pentagonal-numbers) (hexagonal-numbers)]))

Results in heap space overflow exception (at max heap size = 256MB).

I agree that it's a very naive solution, and that finding a third
element of the list will take a lot of time. I still fail to
understand why it's also needing all the memory. I'd be grateful for
any pointers. What am I doing wrong here?


[1] Project Euler problem 45:
http://projecteuler.net/index.php?section=problems&id=45 - I find
solving Project Euler problems is a good way to learn the new
language, while refreshing computer science and math theory at the
same time. If you're interested, you can find my git repository with
39 problems solved here: https://github.com/zuber/project-euler


Thanks,
--
Marek Stępniowski
http://stepniowski.com

-- 
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