On 6/23/06, Pascal Bourguignon <[EMAIL PROTECTED]> wrote:
> 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)))

This is just mind blowing.  I never thought of using lambda's this way
even though I've read Paul Graham's implementation of Scheme's DELAY
and FORCE.

However, considering that this is you, Pascal, I'm surprised that the
code generates a warning :-)

A question though, why didn't you just plug the THEN and ELSE forms
into the lambda like so:

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

Just a guess, but is it so that the compiler can optimize a template
of (lambda (x y) ...) whereas it cannot in this case?

Thanks
Vijay

> --
> __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

Reply via email to