Re: newbie question about quote




wildmind@xxxxxxxxxxx wrote:
> Hi I'm new to LISP. Started to read one article by Paul Graham where he
> says:
>
> "Quote may seem a bit of a foreign concept because few other languages
> have anything like it. It's closely tied to one of the most distinctive
> features of Lisp: code and data are made out of the same data
> structures and the quote operator is the way we distinguish between
> them."
>
> Can anyone give a simple example illustrating what Paul Graham means.

Suppose we were working in C and we have this little function:

void wxMediaStreamOut::PrettyFinish()
{
if (!bad && col) {
f->Write("\n", 1);
col = 0;
}
}

and let us suppose that we didn't want to *run* this function (at
least, not today) but rather we want to do something like print all the
identifiers that refer to methods (in this example, it would be the
identifier `Write'. The identifiers 'f', 'bad' and 'col' refer to
instance members and the identifiers `if', `&&', `->', are built-in.),
or, simpler still, just print the name of the method.

We can't just plop that chunk of code down in the source file because
it would be compiled as a method. We could turn the code into a big
string:

const char * my_method =
"void wxMediaStreamOut::PrettyFinish()\n"
"{\n"
" if (!bad && col) {\n"
" f->Write(\"\\n\", 1);\n"
" col = 0;\n"
" }\n"
"}\n";

But now we need to parse the string into some sort of representation.
This is a pain because the C compiler we are using does this, but it
won't let us get at the internal representation. We could create an
ad-hoc syntax structure that represents the code, but again we are
duplicating effort.

What we *want* is to say ``this here chunk of input that looks just
like a program but I want to pretend it is a data structure".

const Program my_method =
the_program {

void wxMediaStreamOut::PrettyFinish()
{
if (!bad && col) {
f->Write("\n", 1);
col = 0;
}
}

};

The `code' in side the `the_program' construct isn't compiled into a
method, but it will be parsed into a data structure that we can use to
reason about the method we would have compiled. Assuming the `Program'
type had the appropriate interface, we could now do something like
this:

char * name = my_method->getMethodName();

and expect `name' to be bound to the string "PrettyFinish"

This would be pretty slick if we could do it in C, but we can't. A
construct like `the_program' would need to be built in to the C
language and it would require that all C compilers use a standardized
`CodeDOM'.

In Lisp, however, we have something even better (of course) called
quote. Lisp *does* have a standardized `CodeDOM' that happens to be a
subset of the standardized `linked-list' data structure. Quote allows
you to take things that look like programs and treat them like data.
It is trivial to write things like this:

(defvar *my-program*
'(defun pretty-finish ()
(when (and (not bad) col)
(write f "\n" 1)
(setf col 0))))

Extracting the name of the program (like above) is easily done by
calling CADR.

This is a simple, cool, and obvious idea that seems to have been
overlooked by nearly every language out there. Only in the past couple
of years are lanugages coming out with well-defined `CodeDOM's that
allow you do this (although most still don't allow you to embed CodeDOM
objects directly, you need to invoke the parser separately). This idea
was one of the fundamental ideas in Lisp. These new languages are only
about fifty years late to the party.

.



Relevant Pages

  • Re: Glasgow haskell vs. Lispworks
    ... equalp and other data structure specific versions. ... Haskell denies you this particular flexibility, ... The compiler can't discover my intentions. ... At a broad level of generalization, I suppose you could say that Lisp ...
    (comp.lang.functional)
  • lcc-win32
    ... << QUOTE ... It is NOT a C compiler, because it doesn't conform to any commonly ... the most complex data structure I *needed* to use was an array ...
    (comp.lang.c)
  • Re: How Lisps Nested Notation Limits The Languages Utility
    ... same grammar and they both parse to the same data structure. ... structure is neither infix nor prefix. ... In lisp it is hard coded into the compiler ) in a number ...
    (comp.lang.lisp)
  • Re: Python future performance and speed
    ... I didn't know about Lisp, so I did a quick search ... in Google and I found this quote from CMUCL website: ... "a sophisticated native-code compiler which is capable of powerful ... He's been interested in writing a Python compiler for a ...
    (comp.lang.python)
  • Re: Java connected Lisp
    ... versions of GNU Classpath and SBCL, ... Invent a nice and fast interface for calls from Lisp to Java and back. ... The trick could be to hack the compiler so that it recognizes accesses ... Rewrite the "compiler" ...
    (comp.lang.lisp)