Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From my experience, macros make the best impression on people when they see how it helps to produce code, that cannot be made by the procedures or other constructs. Very often such things may be described as:</p> <pre><code>&lt;common code&gt; &lt;specific code&gt; &lt;other common code&gt; </code></pre> <p>where <code>&lt;common code&gt;</code> is always the same. Here are some examples of such schema:</p> <p><strong>1. The <code>time</code> macro.</strong> Code in a language without macros will look something like this:</p> <pre><code>int startTime = getCurrentTime(); &lt;actual code&gt; int endTime = getCurrentTime(); int runningTime = endTime - startTime; </code></pre> <p>You cannot put all common code to procedure, since it wraps actual code around. (OK, you can make a procedure and pass actual code in lambda function, if the language supports it, but it is not always convenient).<br> And, as you most probably know, in Lisp you just create <code>time</code> macro and pass actual code to it: </p> <pre><code>(time &lt;actual code&gt;) </code></pre> <p><strong>2. Transactions.</strong> Ask Java-programmer to write method for simple <code>SELECT</code> with JDBC - it will take 14-17 lines and include code to open connection and transaction, to close them, several nested <code>try-catch-finally</code> statements and only 1 or 2 lines of unique code.<br> In Lisp you just write <code>with-connection</code> macro and reduce code to 2-3 lines. </p> <p><strong>3. Synchronization.</strong> OK, Java, C# and most of the modern languages already have statements for it, but what to do if your language doesn't have such a construct? Or if you want to introduce new kind of synchronization like <a href="http://en.wikipedia.org/wiki/Software_transactional_memory" rel="noreferrer">STM</a>-based transactions? Again, you should write separate class for this task and work with it manually, i.e. put common code around each statement you want to synchronize. </p> <p>That was only few examples. You can mention "not-to-forget" macros like <code>with-open</code> series, that clean-up environment and protect you from recourses leaks, new constructs macros like <code>cond</code> instead of multiple <code>if</code>s, and, of course, don't forget about lazy constructs like <code>if</code>, <code>or</code> and <code>and</code>, that do not evaluate their arguments (in opposite to procedure application). </p> <p>Some programmers may advocate, that their language has a technology to treat this or that case (ORM, AOP, etc), but ask them, would all these technologies be needed, if macros existed? </p> <p>So, taking it altogether and answering original question about how to explain macros. Take any widely-used code in Java (C#, C++, etc), transform it to Lisp and then rewrite it as a macro. </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. 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.
    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