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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:33:17+00:00 2026-05-24T09:33:17+00:00

Say I want to make a Clojure macro that does the following: If x

  • 0

Say I want to make a Clojure macro that does the following:

If x is a list calling the function "bar"
  return :foobar
else
  return x as a string

However, bar is not defined; rather, it is only used internally in the macro, like so:

(foo (bar))
:foobar

(foo 1)
"1"

One could do something like this:

(defmacro foo [x] 
  (if (and (coll? x) (= (first x) 'bar)) 
      :foobar 
      (str x)))

This works great for the (bar) case, as well as for literals. However, symbols do not work as intended, giving the symbol name instead of its associated value:

user=> (def y 2)
#'user/y
user=> (foo y)
"y"

One could call the eval function on x before passing it to str, but this causes problem when using the function in let:

user=> (let [a 3 b (foo a)] b)
java.lang.UnsupportedOperationException: Can't eval locals (NO_SOURCE_FILE:89)

Presumably, the problem has to do with symbol resolution, so maybe we try to work something out with syntax-quote:

(defmacro foo [x] 
  `(if (and (coll? '~x) (= (first '~x) '~'bar)) 
    :foobar 
    (str ~x)))

Now, the problem is with (foo (bar)), as this expands the else clause to (clojure.core/str (bar)), which throws an exception, as bar is not defined. I then tried doing some shenanigans with eval:

(defmacro foo [x] 
  `(if (and (coll? '~x) (= (first '~x) '~'bar)) 
    :foobar 
    (eval '(str ~x))))

But this doesn’t work with let bindings again:

user=> (let [a 1 b (foo a)] b)
java.lang.Exception: Unable to resolve symbol: a in this context (NO_SOURCE_FILE:153)

So I’m really at a loss here. It seems as though fixing one problem breaks another. Is there a better, simpler way of making this macro such that it works in the following cases:

  • In let bindings
  • With (bar)
  • With symbols

P.S. If anybody is curious as to why I want to do this, I’m working on a DSL for Yahoo’s YQL service and I want to be able to do things like (select (table :t) ...), but I need to be able to pass in symbols, as well as literals.

  • 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-24T09:33:19+00:00Added an answer on May 24, 2026 at 9:33 am

    I believe this should work.

    (defmacro foo [x]
      (if (and (coll? x) (= (first x) 'bar))
        :foobar
        `(str ~x)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I want to make a special case for a function that matches
Say we want to make a function like minBy that returns all elements of
Say, i have a function which returns a reference and i want to make
Say I have Div1 and Div2. I want to make that when a user
I want to make a PHP class, lets say Myclass.php. Now inside that class
Let's say I want to make an iOS application that is using new notifications
Let's say I want to make a database that relates emotions with activities. Would
Say I want to make a regex that splits a optional version number from
Let's say I want make some of my sources publicly available via my blog
say I want to make the first row of the excel ss something like

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.