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 9265865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:10:57+00:00 2026-06-18T14:10:57+00:00

I’m a total beginer in Clojure and I’ve ran into a problem that I’m

  • 0

I’m a total beginer in Clojure and I’ve ran into a problem that I’m not even sure if can be done in Closure.

So the issue is the following. I’ve implemented a function that computes the prime numbers from an interval (up to a limit).

(defn gather_primes_in_range [range_start range_end target_number prime_list]
    (if (or (= 0 target_number) (> range_start range_end) (= FIND_MORE_PRIMES false)) 
        prime_list
        (do
            (if (is_prime? range_start)
                (gather_primes_in_range (+ range_start 1) range_end (- target_number 1) (conj, prime_list, range_start))
                (gather_primes_in_range (+ range_start 1) range_end target_number prime_list)
            )
        )
    )
)

(defn find_nr_of_primes_in_range [range_start range_end target_number]
    (if (< range_start 2)
        (gather_primes_in_range 2 range_end target_number [])
        (gather_primes_in_range range_start range_end target_number [])
    )
)

This works just fine. But what I want now is to have a global variable that should store on each method call the primes that are found in a variable to lookup later. In other languages like Python, Ruby or Scala I would just do this by having a Set to which I add entries before returing from the function. But in Clojure I have no ideea how to go around this.

Basically what I tried is, have somewhere global declared:

(def PRIMES_FOUND_SO_FAR #{})

And then somehow on return add the entries to this variable. Is this possible at all in Clojure and if so how? I’ve tried on other variables to change their values using either swap! and atom, or set! but could not make it to work here in any situation.

  • 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-06-18T14:10:58+00:00Added an answer on June 18, 2026 at 2:10 pm

    Firstly, I strongly advice you to read about clojure code conventions What are Clojure's Naming Conventions?

    Let me show you some improvements of your code.

    1) Applying clojure naming conventions.

    Then switch from (+ variable 1) to (inc variable) (the same optimizations with dec).

    Also (= FIND_MORE_PRIMES false) can be simply replaced by find-more-primes?

    And finally the condition (= 0 smthng) could be written in more idiomatic style (zero? smthng)

    Now your code looks a bit more readable:

    (defn gather-primes-in-range [range-start range-end target-number prime-list]
      (if (or (zero? target-number) (> range-start range-end) need-more-primes?)
        prime-list
        (do
          (if (is-prime? range-start)
            (gather-primes-in-range (inc range-start) range-end (dec target-number) (conj prime-list range-start))
            (gather-primes-in-range (inc range-start) range-end target-number prime-list)))))
    

    2) Now we should remove redundant do call cause it wraps the only one function call.

    And the last trick is to apply tail recursion (http://clojure.org/special_forms#Special%20Forms–(recur%20exprs*)) via swapping entire gather-primes-in-range calls to recur

    (defn gather-primes-in-range 
      [range-start range-end target-number prime-list]
        (if (or (zero? target-number) (> range-start range-end) need-more-primes?)
          prime-list
          (if (is-prime? range-start)
            (recur (inc range-start) range-end (dec target-number) (conj prime-list range-start))
            (recur (inc range-start) range-end target-number prime-list))))
    

    And here comes time for answering your question. You wouldn’t benefit from this approach

    (def PRIMES_FOUND_SO_FAR #{})
    

    because you haven’t opportunity to change this set. The only thing that you can deal with it is to create some new immutable data structure from that one.

    As @georgek mention you could simply use atom in this particular case.

    (def PRIMES_FOUND_SO_FAR (atom #{}))
    

    Adding new prime number to atom:

    (swap! PRIMES_FOUND_SO_FAR conj prime-number)
    

    Deref atom for extracting the value:

    @PRIMES_FOUND_SO_FAR ;; or (deref PRIMES_FOUND_SO_FAR)
    -> #{2 3 5 7 11}
    

    Anyway your code is a little bit imperative but you should always remember that clojure is functional language with immutable data structures, functions as arguments, etc. using global variables is not good idea at all. BTW thats how your function should look like in clojure style:

    (defn gather-primes-in-range [start end target-number]
      (take target-number (filter is-prime? (range start end))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.