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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:42:38+00:00 2026-05-14T06:42:38+00:00

I’d like to pre-store a bunch of function calls in a data structure and

  • 0

I’d like to pre-store a bunch of function calls in a data structure and later evaluate/execute them from within another function.

This works as planned for functions defined at namespace level with defn (even though the function definition comes after my creation of the data structure) but will not work with functions defined by let [name (fn or letfn inside the function.

Here’s my small self-contained example:

(def todoA '(funcA))
(def todoB '(funcB))
(def todoC '(funcC))
(def todoD '(funcD)) ; unused

(defn funcA [] (println "hello funcA!"))

(declare funcB funcC)

(defn runit []
    (let [funcB (fn [] (println "hello funcB"))]
    (letfn [(funcC [] (println "hello funcC!"))]
        (funcA)       ; OK
        (eval todoA)  ; OK
        (funcB)       ; OK
        (eval todoB)  ; "Unable to resolve symbol: funcB in this context" at line 2
        (funcC)       ; OK
        (eval todoC)  ; "Unable to resolve symbol: funcC in this context" at line 3
)))

In case you’re wondering about my test setup, to see the result of those 6 statements I comment/uncomment specific of the OK/failing lines and then call (runit) from the REPL.

Is there a simple fix I could undertake to get eval‘d quoted calls to functions to work for functions defined inside another function?


Update:

This (based on danlei’s suggestion) does work. Let’s see if I can get this method working in “real life!”

(def todoB '(funcB))
(declare funcB)

(defn runit []
  (binding [funcB (fn [] (println "hello funcB"))]
    (funcB)
    (eval todoB)  ; "Unable to resolve symbol: funcB in this context" at line 1!
))

Update:

This code is going into my solution for a Constraint Satisfaction Problem – I want to find out who owns the zebra! I’m fairly new to Clojure and especially functional programming, and this has made the exercise quite challenging. I’m falling into a lot of pits but I’m OK with that as it’s part of the learning experience.

I used to specify the constraints as a bunch of simple vectors, like this:

[:con-eq :spain :dog]
[:abs-pos :norway 1]
[:con-eq :kools :yellow]
[:next-to :chesterfields :fox]

where the first of each vector would specify the kind of constraint. But that led me to an awkward implementation of a dispatch mechanism for those rules, so I decided to encode them as (quoted) function calls instead:

'(coloc :japan :parliament) ; 10
'(coloc :coffee :green) ; 12
'(next-to :chesterfield :fox) ; 5

so I can dispatch the constraining rule with a simple eval. This seems a lot more elegant and “lisp-y.” However, each of these functions needs to access my domain data (named vars), and this data keeps changing as the program runs. I didn’t want to blemish my rules by introducing an extra argument, so I wanted vars to be available to the eval‘d functions via dynamic scoping.

I’ve now learned that dynamic scoping can be done using binding, but it also needs a declare.

  • 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-14T06:42:38+00:00Added an answer on May 14, 2026 at 6:42 am

    Do you mean something like this?

    (def foo '(bar))
    (declare bar)
    
    (binding [bar (fn [] (println "hello bar"))]
      (eval foo))
    

    If yes, your problem reduces to this:

    (let [foo 1]
      (eval 'foo))
    

    This won’t work, because eval does not evaluate in the lexical environment. You can get around that using vars:

    (declare foo)
    
    (binding [foo 1]
      (eval 'foo))
    

    As far as that is concerned, Clojure seems to have similar semantics to CL, cf. the CLHS:

    Evaluates form in the current dynamic environment and the null lexical environment.

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

Sidebar

Ask A Question

Stats

  • Questions 360k
  • Answers 360k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your second box may have a broken Blas / Lapack… May 14, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer You can't. Window.focus is disabled in Chrome for security reasons,… May 14, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer There is a better implementation as behavior. http://www.yiiframework.com/forum/index.php?/topic/6905-please-test-my-ar-enhancement-automatically-sync-many-many-table-when-calling-save/ May 14, 2026 at 2:38 pm

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.