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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:14+00:00 2026-05-22T22:19:14+00:00

In Clojure, (def x 3) (eval ‘(prn x)) prints 3, whereas (let [y 3]

  • 0

In Clojure,

(def x 3)
(eval '(prn x))

prints 3, whereas

(let [y 3]
   (eval '(prn y)))

and

(binding [z 3] (eval '(prn z)))

generate an ‘Unable to resolve var’ exception.

According to http://clojure.org/evaluation, eval, load-string, etc generate temporary namespaces to evaluate their contents. Therefore, I’d expect neither of the above code samples to work, since (def x 3) is done in my current namespace, not the one created by eval.

  1. Why does the first code sample work and not the last two?
  2. How can I eval a form with bound variables without using def?

Thanks!

  • 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-22T22:19:15+00:00Added an answer on May 22, 2026 at 10:19 pm

    1.:

    The reason this doesn’t work is (more or less) given on the page you linked:

    It is an error if there is no global var named by the symbol […]
    

    And:

    […]

    1. A lookup is done in the current namespace to see if there is a mapping
      from the symbol to a var. If so, the
      value is the value of the binding of
      the var referred-to by the symbol.

    2. It is an error.

    eval evaluates forms in an empty (null in CL-lingo) lexical environment. This means, that you cannot access lexical variable bindings from the caller’s scope. Also, binding creates new bindings for existing vars, which is why you cannot use it “by itself”, without having declared or defed the variables you try to bind. Besides, lexical variables (at least in CL, but I would be surprised if this wasn’t the case for Clojure) already ceased to exist at runtime – They are translated to addresses or values.

    See also my older post about this topic.

    2.:

    So, you have to use dynamic variables. You can avoid the explicit def, but you still at least need to declare them (which defs var names without bindings):

    user=> (declare ^:dynamic x)
    #'user/x
    user=> (binding [x 10] (eval '(prn x)))
    10
    nil
    

    By the way: I suppose you know why you need eval, and that its use is considered evil when other solutions would be appropriate.

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

Sidebar

Related Questions

In Clojure you can unquote-splice a list of values to generate code, e.g. (def
I'm calling the twitter4j library using Clojure like so: (def twitter (. (TwitterFactory.) getInstance))
Is there a simpler way to write this code in Clojure : (def queue
I am looking through some example Fibonacci sequence clojure code: (def fibs (lazy-cat [1
Why are some (many) of Clojure's built-in functions defined using def , not defn
I've just started learning Clojure, if I define the following map: (def distributions {:normal
How can I get a clojure def to re-evaluate without using :reload?
In clojure I have lines like this that define default values: (def *http-port* 8080)
I have the following defined in clojure: (def ax '(fn x [] (+ 1
Clojure 1.2.0 user=> (def x (repeat 10 1)) #'user/x user=> (counted? x) false user=>

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.