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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:47:34+00:00 2026-05-28T02:47:34+00:00

Clojure macro noob here. I have a function with some optional parameters, e.g. (defn

  • 0

Clojure macro noob here. I have a function with some optional parameters, e.g.

(defn mk-foo [name & opt]
  (vec (list* name opt)))

giving this:

user> (mk-foo "bar" 1 2 3)
["bar" 1 2 3]

I’m trying to write a macro which takes the same optional arguments and passes them transparently to an invocation of mk-foo. So far I have this:

(defmacro deffoo [name & opt]
  `(def ~name ~(apply mk-foo (str name) opt)))

which has the desired effect:

user> (macroexpand '(deffoo bar 1 2 3))
(def bar ["bar" 1 2 3])

The use of apply to flatten the list opt feels clumsy. Is there an idiomatic way to do this? I’m guessing ~@ is needed, but I can’t get the quoting right. Many 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-28T02:47:34+00:00Added an answer on May 28, 2026 at 2:47 am

    Your intuition about using apply served you well in this case. When you have a quoted form ` and then unqote all of them it can help to think about moving the un-quoting down to the smallest part or the list. This avoids using code to generate forms that could be simply written.

    user=> (defmacro deffoo [name & opt] `(def ~name [~(str name) ~@opt]))       
    #'user/deffoo
    user=> (macroexpand '(deffoo "bar" 1 2 3))                            
    (def "bar" ["bar" 1 2 3])
    

    and here it is with the call to mk-foo:

    (defmacro deffoo [name & opt] `(def ~name (mk-foo ~(str name) ~@opt)))     
    #'user/deffoo
    user=> (macroexpand '(deffoo "bar" 1 2 3))                                   
    (def "bar" (user/mk-foo "bar" 1 2 3))
    

    in this second case we move the ~ in one level and let the call to mk-foo stay quoted and only unquote the args required to build the parameter list (using splicing-unquote as you suspected)

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

Sidebar

Related Questions

Is there anything in Clojure that is equivalent to Common Lisp's symbol-name function?
I'm having some trouble understanding how the delay macro works in Clojure. It doesn't
I have a macro that takes a body: (defmacro blah [& body] (dostuffwithbody)) But
I am using clojure and hiccup (with noir) and I have this code: (defn
So following on from Clojure macro to create a synonym for a function ,
This is my first Clojure macro -- I am an uber-noob. Yesterday I posted
I have a clojure macro: (defmacro show [x] `(show-fn ~x) ) : which given
I'm trying to create some dynamic code within clojure. In the function below, the
How can I cause a Clojure macro to act as a function, so I
I'm trying to write a clojure macro that lets me call a function and

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.