Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:57:33+00:00 2026-05-16T05:57:33+00:00

I have a question about Clojure: I am trying to learn the language by

  • 0

I have a question about Clojure:
I am trying to learn the language by going through Project Euler and I don’t understand what is going on under the hood: The following code is designed to use return a list of all prime numbers up to lim. I would think that it should be O(n) in heap-space because I make a list of all the numbers up to lim, and then filter them away one by one while moving the first to a new list. (I know that I am actually making new lists each recur but I didn’t think they would take more memory?) Anyway I am running this with

(defn getAllPrimes [lim] 
  (defn getPrimes [primes numlist]
    (if (not-empty numlist) ;base case;
    (recur (cons (first numlist) primes) ;put the prime on to the prime list 
           (filter
        (fn [x] (not (div? x (first numlist)))) ;remove the prime and all its multiples from the numlist
        (rest numlist)))
      primes)); return the primes
  (getPrimes () (range 2 lim))) ;call the recursive function with and empty prime list to be filled up and a full numlist to be emptied

And I keep running out of heap space, when I call

(apply + (getAllPrimes 2000000))

, but I don’t run out of space on

(apply + (filter even? (range 2000000)))

So I think that I must not understand how lists are garbage collected in calls to recur and am actually using O(n*n) heap or something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:57:34+00:00Added an answer on May 16, 2026 at 5:57 am

    I believe the problem is that with each recur, you’re creating a new lazy sequence referring to the last one, so after a few iterations you’re holding a seq that holds the head of a seq that holds the head of a seq that holds the head of a seq. … All the intermediate seqs are filling up your heap.

    Though writing a prime sieve is a worthwhile exercise, if you want to get to the answer, Clojure does include the sequence of prime numbers in its standard library: clojure.contrib.lazy-seqs/primes. The standard solution to this paricular Euler problem is a one-liner.

    As a style point, an inner defn is not a good idea. The practical effect is the same as if the defn were at the top level, but if I’m not mistaken, the var gets reassigned every time getAllPrimes is called, and redefining vars at runtime is very strongly discouraged. Since the code is just defining a var, getPrimes is still just as visible as getAllPrimes. In this case, getPrimes could easily be rewritten as a loop/recur with no inner function, anonymous or named. That doesn’t help your chain-of-lazy-seqs problem, but it does make the code a little more standard-looking:

    (defn getAllPrimes [lim]
      (loop [primes ()
             numlist (range 2 lim)]
        (if (not-empty numlist)
          (recur (cons (first numlist) primes)
                 (filter (fn [x] (not (div? x (first numlist))))
                         (rest numlist)))
          primes)))
    

    I would also avoid the use of camelCase. The Clojure standard name for this function would be get-all-primes.

    Getting back to the practical problem, though, the least work you could do to get your code working would be to force each seq on each iteration, i.e., wrap your filter call in a doall. I tried this, and while it still runs slowly, it at least does run to completion without running out of heap:

    (defn get-all-primes [lim]
      (loop [primes ()
             numlist (range 2 lim)]
        (if (not-empty numlist)
          (recur (cons (first numlist) primes)
                 (doall (filter #(not (div? % (first numlist)))
                                (rest numlist))))
          primes)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about Clozure CL. While I was trying to quickload a
The point of this question is to clear up confusion about Clojure project.clj dependencies
I am asking this question to straighten out a confusion I have about project.clj
I have a question about iterate and Clojure library funcs implemented similarly to iterate.
I have a couple of questions about Hello World in Clojure: (println Hello, world!)
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.