Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>defn</code> defines a function, <code>defmacro</code> defines a macro.</p> <p>The difference between functions and macros is that on a function call first the arguments of the function are evaluated then the body of the function is evaluated using the arguments.</p> <p>Macros on the other hand describe a transformation from one piece of code to another. Any evaluation takes place after the transformation.</p> <p>This means that arguments may be evaluated multiple times or not at all. As an example <code>or</code> is a macro. If the first argument of <code>or</code> is false, the second argument will never be evaluated. If <code>or</code> were a function, this would not be possible, because the arguments would always be evaluated before the function runs.</p> <p>Another consequence of this is that the arguments of a macro need not be a valid expression before the macro is expanded. For example you could define a macro <code>mymacro</code> such that <code>(mymacro (12 23 +))</code> expands to <code>(+ 23 12)</code>, so this will work even though <code>(12 23 +)</code> on its own would be nonsense. You couldn't do that with a function because <code>(12 23 +)</code> would be evaluated, and cause an error, before the function runs.</p> <p>A small example to illustrate the difference:</p> <pre><code>(defmacro twice [e] `(do ~e ~e)) (twice (println "foo")) </code></pre> <p>The macro <code>twice</code> gets the list <code>(println "foo")</code> as an argument. It then transforms it into the list <code>(do (println "foo") (println "foo"))</code>. This new code is what gets executed.</p> <pre><code>(defn twice [e] `(do ~e ~e)) (twice (println "foo")) </code></pre> <p>Here <code>println "foo"</code> is evaluted right away. Since <code>println</code> returns <code>nil</code>, twice is called with <code>nil</code> as its argument. <code>twice</code> now produces the list <code>(do nil nil)</code> and returns that as its result. Note that here <code>(do nil nil)</code> is not evaluated as code, it's just treated as a list.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload