12
votes

I was practicing writing macros and I can't seem to get defn to work.

My syntax is: (my-define name parameter body)

Ignoring & parameters and recursive routines, How do I bind the name to a (fn[parameter] body)?

1
You can use macroexpand to see in the repl what a macro expands to. E.g. (macroexpand '(defn foo [] 1)). This is useful both in debugging your own macros, as well as trying to learn from the clojure/core macros.liwp
May also use (source defn) to directly inspect the source code.dqc

1 Answers

24
votes

You will need to transform

(my-define <name> <args> <body>)

to

(def <name> (fn <args> <body>))

This is quite simple actually:

(defmacro my-define [name args body]
  `(def ~name (fn ~args ~body)))