Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

5. Macros > Syntax

Syntax

Since macros return Clojure data structures, we will often want to return lists to represent further calls, either of functions, special forms, or other macros. Therefore, we need tools to build these lists. It’s perfectly acceptable to use the simplest tool in our toolbox, the list function:

(defmacro hello
  [name]
  (list 'println name))

(macroexpand '(hello "Brian"))
;= (println "Brian")

However, for more complex macros that return something more than a simple flat list, this quickly becomes unwieldy. This is how the source code for the standard macro while would look, written in this manner:

(defmacro while
  [test & body]
  (list 'loop []
    (concat (list 'when test) body)
      '(recur)))

The gist of this macro is lost in all the calls to list and concat.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial