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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:48:43+00:00 2026-05-11T21:48:43+00:00

OK. I’ve been tinkering with Clojure and I continually run into the same problem.

  • 0

OK. I’ve been tinkering with Clojure and I continually run into the same problem. Let’s take this little fragment of code:

(let [x 128]
  (while (> x 1)
    (do
      (println x)
      (def x (/ x 2)))))

Now I expect this to print out a sequence starting with 128 as so:

128
64
32
16
8
4
2

Instead, it’s an infinite loop, printing 128 over and over. Clearly my intended side effect isn’t working.

So how am I supposed to redefine the value of x in a loop like this? I realize this may not be Lisp like (I could use an anonymous function that recurses on it’s self, perhaps), but if I don’t figure out how to set variable like this, I’m going to go mad.

My other guess would be to use set!, but that gives “Invalid assignment target”, since I’m not in a binding form.

Please, enlighten me on how this is supposed to work.

  • 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-11T21:48:43+00:00Added an answer on May 11, 2026 at 9:48 pm

    def defines a toplevel var, even if you use it in a function or inner loop of some code. What you get in let are not vars. Per the documentation for let:

    Locals created with let are not variables. Once created their values never change!

    (Emphasis not mine.) You don’t need mutable state for your example here; you could use loop and recur.

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

    If you wanted to be fancy you could avoid the explicit loop entirely.

    (let [xs (take-while #(> % 1) (iterate #(/ % 2) 128))]
      (doseq [x xs] (println x)))
    

    If you really wanted to use mutable state, an atom might work.

    (let [x (atom 128)]
      (while (> @x 1)
        (println @x)
        (swap! x #(/ %1 2))))
    

    (You don’t need a do; while wraps its body in an explicit one for you.) If you really, really wanted to do this with vars you’d have to do something horrible like this.

    (with-local-vars [x 128]
      (while (> (var-get x) 1)
        (println (var-get x))
        (var-set x (/ (var-get x) 2))))
    

    But this is very ugly and it’s not idiomatic Clojure at all. To use Clojure effectively you should try to stop thinking in terms of mutable state. It will definitely drive you crazy trying to write Clojure code in a non-functional style. After a while you may find it to be a pleasant surprise how seldom you actually need mutable variables.

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

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess you are trying to pass a pointer to… May 12, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer Use jQuery. DOM differences between browsers is one of the… May 12, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer Because you have no records in your DB with CONFIRMED… May 12, 2026 at 2:59 pm

Related Questions

OK, I know what you're thinking, "why write a method you do not want
Ok, I asked a question earlier about Flex and ADO.NET Data Services but didn't
Ok, I have been working with Solaris for a 10+ years, and have never
OK, I know there have already been questions about getting started with TDD ..
Ok, I have a strange exception thrown from my code that's been bothering me

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.