concept macroexpand-1 in category clojure

appears as: macroexpd-1
Clojure: The Essential Reference

This is an excerpt from Manning's book Clojure: The Essential Reference.

memfn expands into an anonymous function definition that can be seen if we “macroexpand, macroexpand-1 and macroexpand-all” the form:

We can verify the expected behavior with macroexpand-1 which for clarity is not further expanding past the first level:

Writing macros can sometimes be a haunting experience and debugging a misbehaving one can be a true nightmare. When one finds themself in either situation, the set of macro expanding functions macroexpand, macroexpand-1 and clojure.walk/macroexpand-all are the tools to reach for. The set of macro expanding functions are almost exclusively used at the REPL, either to understand how a macro works or as an aid when writing or debugging one. They all perform the macro expansion steps on the input form, without evaluating the result but instead returning it:

(macroexpand-1 '(when false (println "this will never be printed!"))) ; #1
;; (if false (do (println "this will never be printed!")))

The difference between macroexpand-1, macroexpand and clojure.walk/macroexpand-all is in how deep they will macro expand the form before returning:

  • macroexpand-1 just performs macro expansion on the outermost form and returns immediately (thus the -1 suffix).