cl-faq  

Re: [cl-faq] faq answer: What's the difference between a function, a macro, and a special form?

Pascal Bourguignon
Fri, 23 Jun 2006 09:02:36 -0700

Vijay Lakshminarayanan writes:
> I think this is a very good explanation (though my opinion doesn't
> carry much weight, I'll admit :-).
> 
> One thing, IMHO, is you could explain why a special form (such as IF)
> cannot be written using a macro.  I remember reading about that in a
> book or a tutorial and I think it would be apt here too.

Well, actually it can be written as a macro.  


First, the language specifiers could make the other choice of which
will be the special operator and which the macro:

    Special Operator IF
    Macro COND

or

    Macro IF
    Special Operator COND

(shadow 'if)
(defmacro if (test then else) `(cond (,test ,then) (t ,else)))

In CL, the first choice is specified.



Then, you could just go back to lambda stuff:

(shadow 'if)
(defmacro if (test then else)
   `(funcall (cdr (assoc (not ,test) `((t   . ,(lambda (x y) (funcall y)))
                                       (nil . ,(lambda (x y) (funcall x)))))) 
         (lambda () ,then) (lambda () ,else)))

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

CAUTION: The mass of this product contains the energy equivalent of
85 million tons of TNT per net ounce of weight.
_______________________________________________
cl-faq mailing list
cl-faq@lispniks.com
http://www.lispniks.com/mailman/listinfo/cl-faq