Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How might I write a "defn" macro in Clojure?

Tags:

macros

clojure

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)?

like image 817
unj2 Avatar asked Jul 25 '09 15:07

unj2


People also ask

Does Clojure have macros?

Clojure has a programmatic macro system which allows the compiler to be extended by user code. Macros can be used to define syntactic constructs which would require primitives or built-in support in other languages. Many core constructs of Clojure are not, in fact, primitives, but are normal macros.


1 Answers

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)))
like image 123
Jonas Avatar answered Sep 25 '22 16:09

Jonas