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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:25:59+00:00 2026-06-17T01:25:59+00:00

Ok so I have this macro that is supposed to take a varying number

  • 0

Ok so I have this macro that is supposed to take a varying number of arguments and then execute them with try and catch. I am assuming that if the argument list arg-list is bigger then 2 then the first element in the list is a binding, like this [a 0] for example. So arg-list may look like this: ([s (FileReader. (File. "text.txt"))] (. s read)).

This is what I’ve come up with:

(defmacro safe [& arg-list] (list 'if (list '< (list 'count arg-list) '2)
    (list 'try (list 'eval arg-list) (list 'catch 'Exception 'e 'e))
    (list 'do (list 'eval arg-list) (list 'try (list 'eval (list 'rest arg-list)) (list 'catch 'Exception 'e 'e)))))

I have been struggling to get this to work for like two straight days now, but it never works. When I try this macro with for example this:

(safe (+ 2 3))

i get this error:

ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn  user/eval91 (NO_SOURCE_FILE:100)

I have only been working with Clojure for four days so forgive me if my code is bad.

  • 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-06-17T01:26:00+00:00Added an answer on June 17, 2026 at 1:26 am

    Well… for a start I suggest you read up on clojure’s macro syntax. I’ll provide a bit of a primmer here but I’m not going to go into depth.

    First things first, here’s your macro.

    (defmacro safe [bindings? & forms]                                              
      (let [bindings (if (and (even? (count bindings?)) (vector? bindings?))        
                       bindings? nil)                                               
            forms    (if bindings forms (cons bindings? forms))                     
            except  `(catch Exception e# e#)]                                       
        (if bindings                                                                
        `(let ~bindings (try ~@forms ~except))                                      
        `(try ~@forms ~except))))
    

    And now for a walk through.

    Clojure’s (let) macro demands a vector with an even number of arguments and supports some really interesting behavior called destructuring. For the purposes of this macro, I assume that any valid binding argument will first be a vector and second be of even length. The evaluation of (let) will perform this same check, but this macro must do it as it’s possible that the first form is not a binding but a form to be evaluated and should exhibit different behavior in that case.

    As to the macro itself, I use a (let) to process the arguments, the symbol bindings serving the double purpose of indicating the presence of bindings as well as taking the binding vector if one is present. forms is re-defined from its initial binding in the arguments (clojure lets you do that) to a value which is impacted by that of bindings being the entire form sequence which you wish to execute in an error-contained environment. The except symbol really isn’t called for, it’s just to escape the code duplication of restating that (catch) form in each of the expansion cases.

    The symbol ` (known as backquote or backtick) which I use is equivalent here to normal quote (‘) except that clojure allows me to use the macro expansion syntax within backquoted forms and not quoted forms. The macro syntax contains the ~ (unquote) operator and the ~@ (insert (unquote)) uperator. Using these three bits of notation I’ve defined both desired cases, the let with a binding form where I insert the binding form and the forms to be tried and the simple try only case.

    The conditional could be eliminated to produce

    (defmacro safe [bindings? & forms]                                              
      (let [bindings (if (and (even? (count bindings?)) (vector? bindings?))        
                       bindings? [])                                               
            forms    (if-not (empty? bindings) 
                       forms (cons bindings? forms))                     
            except  `(catch Exception e# e#)]                                      
        `(let ~bindings (try ~@forms ~except))))
    

    but then you have a superfluous (let) when there is no binding form.

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

Sidebar

Related Questions

I have an application that used Macro. In this macro I would like to
I have a macro that is dynamically generated before compilation (it's supposed to contain
I have this macro (from the clojure-koans ) that should allow the use of
I have found this macro while digging in the source code of tweejump game.
If you have a macro like this in C: #define SLICE_VERSION 20110614 How to
I have a macro definition in header file like this: // header.h ARRAY_SZ(a) =
I have started writing a Macro in Visual Studio 2005 like this: Public Sub
Suppose that I have recorded a long macro with many commands and special characters.
I have this macro: #define kPaxZoneW 105.0 I want to define a subsequent macro
I have 2 macros below that I am trying to execute 1 after another

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.