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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:12:32+00:00 2026-06-02T22:12:32+00:00

If I understand correctly Clojure can return lists (as in other Lisps) but also

  • 0

If I understand correctly Clojure can return lists (as in other Lisps) but also vectors and sets.

What I don’t really get is why there’s not always a collection that is returned.

For example if I take the following code:

(loop [x 128]
  (when (> x 1)
    (println x)
    (recur (/ x 2))))

It does print 128 64 32 16 8 4 2. But that’s only because println is called and println has the side-effect (?) of printing something.

So I tried replacing it with this (removing the println):

(loop [x 128]
  (when (> x 1)
    x
    (recur (/ x 2))))

And I was expecting to get some collecting (supposedly a list), like this:

(128 64 32 16 8 4 2)

but instead I’m getting nil.

I don’t understand which determines what creates a collection and what doesn’t and how you switch from one to the other. Also, seen that Clojure somehow encourages a “functional” way of programming, aren’t you supposed to nearly always return collections?

Why are so many functions that apparently do not return any collection? And what would be an idiomatic way to make these return collections?

For example, how would I solve the above problem by first constructing a collection and then iterating (?) in an idiomatic way other the resulting list/vector?

First I don’t know how to transform the loop so that it produces something else than nil and then I tried the following:

(reduce println '(1 2 3))

But it prints “1 2nil 3nil” instead of the “1 2 3nil” I was expecting.

I realize this is basic stuff but I’m just starting and I’m obviously missing basic stuff here.

(P.S.: retag appropriately, I don’t know which terms I should use here)

  • 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-02T22:12:34+00:00Added an answer on June 2, 2026 at 10:12 pm

    A few other comments have pointed out that when doesn’t really work like if – but I don’t think that’s really your question.

    The loop and recur forms create an iteration – like a for loop in other languages. In this case, when you are printing, it is indeed just for the side effects. If you want to return a sequence, then you’ll need to build one:

    (loop [x 128                                                                                                        
           acc []]                                                                                                      
      (if (< x 1)                                                                                                       
        acc                                                                                                             
        (recur (/ x 2)                                                                                                  
               (cons x acc))))                                                                                          
    
    => (1 2 4 8 16 32 64 128)
    

    In this case, I replaced the spot where you were calling printf with a recur and a form that adds x to the front of that accumulator. In the case that x is less than 1, the code returns the accumulator – and thus a sequence. If you want to add to the end of the vector instead of the front, change it to conj:

    (loop [x 128                                                                                                    
           acc []]                                                                                                  
      (if (< x 1)                                                                                                   
        acc                                                                                                         
        (recur (/ x 2)                                                                                              
               (conj acc x))))                                                                                      
    
    => [128 64 32 16 8 4 2 1] 
    

    You were getting nil because that was the result of your expression — what the final println returned.

    Does all this make sense?

    reduce is not quite the same thing — it is used to reduce a list by repeatedly applying a binary function (a function that takes 2 arguments) to either an initial value and the first element of a sequence, or the first two elements of the sequence for the first iteration, then subsequent iterations are passed the result of the previous iteration and the next value from the sequence. Some examples may help:

    (reduce + [1 2 3 4])                                                                                            
    
    10  
    

    This executes the following:

    (+ 1 2) => 3
    (+ 3 3) => 6
    (+ 6 4) => 10
    

    Reduce will result in whatever the final result is from the binary function being executed — in this case we’re reducing the numbers in the sequence into the sum of all the elements.

    You can also supply an initial value:

    (reduce + 5 [1 2 3 4])                                                                                            
    
    15  
    

    Which executes the following:

    (+ 5 1)  => 6
    (+ 6 2)  => 8
    (+ 8 3)  => 11
    (+ 11 4) => 15
    

    HTH,

    Kyle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

really can't understand, how to build correctly project that uses boost.python. I've included boost_(python/thread/system)-mt.
If I understand correctly, typeid can determine the actual type in polymorphism, while typeof
If I understand correctly, scala.util.control.TailCalls can be used to avoid stack overflows for non-tail-recursive
I'm new to Java but if I understand correctly, a char is a primitive.
If I understand correctly, UTF-32 can handle every character in the universe. So can
If I understand correctly Groovy is dynamically typed but since it's almost a superset
Perhaps I do not understand correctly how MVC Areas work, but this has got
If I understand correctly, I can use nServiceBus as a framework and / or
If I understand correctly, in .NET the default implementation of Object.GetHashCode() returns a value
Do I understand correctly that CCLabelBMFont only loads the font texture once, no matter

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.