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

  • Home
  • SEARCH
  • 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 9214859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:04:00+00:00 2026-06-18T02:04:00+00:00

I want to create several items for each call to a recursive function, and

  • 0

I want to create several items for each call to a recursive function, and collect everything in a list. That is, I want to do something like this:

(defn myfunc [x]
  (loop [x x retur '()]
    (when condition1
      (let [templist '()]
        (if condition2 (def templist (conj templist {:somekey someval})))
        (if condition3 (def templist (conj templist {:somekey someval})))
        (recur (+ x 1) (concat retur templist))))))

The problem is that in Clojure I can’t rebind a let. I would like to avoid using global variables.

  • 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-18T02:04:01+00:00Added an answer on June 18, 2026 at 2:04 am

    Some of the functions in core use this pattern of chaining the same symbol through a let to conditionally build up a value. I had to change contition1 to an example that would not loop forever, and change the when to an if so it could return a value at the end of the loop.

    (defn myfunc [x someval1 someval2 condition1 condition2 condition3]
      (loop [x x retur '()]
        (if (condition1 x)
          (let [templist '()
                templist (if condition2 (conj templist {:somekey1 someval1}) templist)
                templist (if condition3 (conj templist {:somekey2 someval2}) templist)]
            (recur (+ x 1) (concat retur templist)))
          retur)))
    

    which can then be tested:

     user> (myfunc 0 1 2 #(< % 5) true true)
     ({:somekey2 2} {:somekey1 1} {:somekey2 2} {:somekey1 1} {:somekey2 2} 
      {:somekey1 1} {:somekey2 2} {:somekey1 1} {:somekey2 2} {:somekey1 1})
    
    user> (myfunc 0 1 2 #(< % 5) true false)
    ({:somekey1 1} {:somekey1 1} {:somekey1 1} {:somekey1 1} {:somekey1 1})
    

    The idea in the let is to have each stage change the value if the condition is true, or return it unchanged if the condition is false. This pattern gives functional code an imperative look and can help to make it clear how a value is constructed, though it can also be taken too far in using it to “convert” imperative logic into a functional program.

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

Sidebar

Related Questions

I want to create a widget that contains several QDockWidgets on purpose of putting
I want to create a loop that will iterate over several strings, but unable
I want to create a listview where each element has several imageview, but they
I have a model that looks something like this: public class SampleModel { public
I want to append several js files dynamically. Each file has a function definition
i was requested to make in android a view that groups several items like
I have several UIImages and I want to create a video from them. I
We have build several web service based on .net. Now we want to create
I have a data frame with several variables. What I want is create a
when i create a thread, i want to pass several arguments. So i define

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.