so, i&#x27;m not a CL wizard, but i&#x27;ve spent some time with it. One cool part about macros, the part that really matters, you get to control the order of evaluation. That doesn&#x27;t sound super interesting, so let&#x27;s get a little more concrete.<p>Say you want to model option contracts. They can get crazy complicated. most DSLs will have things that are AND like and things that are OR like. In C you&#x27;re stuck with something like this for the and case:<p><pre><code>    all = 1;
    for(int i = 0; i &lt; clause_count; i++){
        if(eval(clause[i]) == 0){
            all = 0;   
            break;
        }
    }
    return all;
</code></pre>
The key point here is the break. we actually want to stop evaluation of the later parts of the code.<p>with a macro, you just expand out all of the eval&#x27;ing of the clauses in place. you can&#x27;t just map over the list of clauses, because we want the early exit.<p>my lisp is rusty, but i think it&#x27;d look kinda like<p><pre><code>    (defmacro optionand (&amp;rest clauses) 
        `(and ,@(map (lambda (c) (list &#x27;eval c) clauses)))
</code></pre>
the gist being, we leverage the built in and to do something like:<p><pre><code>    (and
        (eval google-at-700)
        (eval oil-at-50))
</code></pre>
so you embed right in the language itself, taking advantage of the built in short circuiting of and.<p>in haskell, you can play a very cute game. since functions are lazy, you get full control over evaluation - you don&#x27;t need a special form to implement if, for example.<p>So, the very cute trick is to make your dsl out of ordinary functions, that use typeclasses for implementation. in the above case eval would be a method in a typeclass. (you can do the very same thing in lisp, it just seems like a bit more effort to carry around the specific implementation of eval, haskell will just figure it out with the typechecker).<p>The thing that&#x27;s great about the trick, you can write multiple implementations of eval, and not rewrite all your contracts. One implementation could just straight up evaluate it. Another version could do some fuzzier math with variance of the underlying price, and return a gaussian rather than a number.<p>Again, the key is controling the actual evaluation i think there&#x27;s some bits and pieces about it in _on lisp_ which i think pg made available online.
haskell&#x27;s bracket function is a great example and would be a nice macro candidate<p><pre><code>    (bracket obtain-resource do-stuff release-resource)
    (bracket get-socket talk close-socket) ; for example
</code></pre>
no matter what happens in do-stuff, release is always called.<p>it&#x27;s possible (even easy?) to do scary stuff with controlling the order of evaluation, but when you see code with those complicated breaks and continues, it&#x27;s often a side effect of not having good macros.<p><i>edit</i><p>i learned the haskell trick from Oleg Kiselyov&#x27;s papers - <a href="http:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;tagless-final&#x2F;" rel="nofollow">http:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;tagless-final&#x2F;</a> He uses it to make an interpreter into a compiler, and very clever stuff with partial evaluation. That guy is really smart.<p>Although pg doesn&#x27;t call out DSL&#x27;s specifically, the macro chapter is good. <a href="http:&#x2F;&#x2F;unintelligible.org&#x2F;onlisp&#x2F;onlisp.html#SEC49" rel="nofollow">http:&#x2F;&#x2F;unintelligible.org&#x2F;onlisp&#x2F;onlisp.html#SEC49</a> and indeed you can get the whole book here <a href="http:&#x2F;&#x2F;www.paulgraham.com&#x2F;onlisp.html" rel="nofollow">http:&#x2F;&#x2F;www.paulgraham.com&#x2F;onlisp.html</a>