Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Syntax issues</h2> <p>Yes, you can have Lisp-like macros in an imperative language, because Lisp supports imperative programming. The main difference between macros in C and Lisp is how easy it is to manipulate the source tree:</p> <ul> <li><p>In C, there are declarations, declarators, statements, expressions, blocks, a handful of distinct control structures, labels, etc. New syntactic constructs might require changes to the parser. Macros will need to construct these data structures.</p></li> <li><p>In Lisp, there are only s-expressions. New syntactic constructs require no changes to the parser. Only one data structure means the API for constructing a syntax tree is very simple and easy to remember.</p></li> </ul> <p>There are some languages with more complicated syntax (like C) but which have powerful macro facilities (like Lisp). For example, Haskell. However, the interface for writing macros in Haskell is somewhat more complicated, since you need functions for creating and applying type constructors, expressions, declarations, expressions, etc., instead of just a single constructor for lists.</p> <p>A template in a macro in Haskell has its type annotated:</p> <pre><code>[e| ... |] -- expression [d| ... |] -- declaration [t| ... |] -- type [p| ... |] -- pattern </code></pre> <p>By comparison, those letters <code>e</code>, <code>d</code>, <code>t</code>, and <code>p</code> are not needed in Lisp macros. These are necessary in Haskell <strong>not</strong> because Haskell is strongly typed, but because the annotations put the parser in the correct state so it can parse the contents with the proper context. Again, the Lisp syntax only has one context.</p> <h2>Interpreted versus compiled</h2> <p>Most languages can be interpreted, compiled, or both at the same time. C can be either or both. Lisp can be either or both. Macros require the compiler to execute code at compile-time, which can be done either by interpreting the macro code, or by compiling the macro and then executing it. So interpreted-versus-compiled is really a non-issue (it is a non-issue in almost every discussion about languages).</p>
 

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