From: ········@fastmail.fm
Subject: newbie question about quote
Date: 
Message-ID: <1136589121.572587.229930@f14g2000cwb.googlegroups.com>
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.

Wild Mind

From: Tayssir John Gabbour
Subject: Re: newbie question about quote
Date: 
Message-ID: <1136630748.819677.154400@o13g2000cwo.googlegroups.com>
········@fastmail.fm 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.
>
> Wild Mind


Some bits of code evaluate to themselves.

CL-USER> 1
1

CL-USER> "this is a string"
"this is a string"

CL-USER> :keyword
:KEYWORD


However, others are evaluated differently, like symbols and lists. You
quote them in order for Lisp not to evaluate them in that manner.


CL-USER> (quote this-is-a-symbol)
THIS-IS-A-SYMBOL

CL-USER> (quote (list o stuff))
(LIST O STUFF)


Quoting the stuff which evaluate to themselves doesn't quite have the
same fireworks.

CL-USER> (quote 1)
1

CL-USER> (quote "this is a string")
"this is a string"

CL-USER> (quote :keyword)
:KEYWORD


But quoting it twice does.

CL-USER> (quote (quote 1))
(quote 1)

Actually, it says '1, but 'x and (quote x) are identical.


Tayssir
From: GB
Subject: Re: newbie question about quote
Date: 
Message-ID: <hvMvf.37411$9G.3755@dukeread10>
········@fastmail.fm 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.
> 
> Wild Mind
> 

The expression

(+ 1 2)

can be treated as a form to be evaluated or as a list of the tokens +, 
1, and 2. The use of quote determines which of these two interpretations 
lisp should make.

(+ 1 2) ==> 3
(car (+ 1 2)) ==> error (can't take car of 3)

'(+ 1 2) ==> (+ 1 2)
(car '(+ 1 2)) ==> +

Gregg
From: Kenny Tilton
Subject: Re: newbie question about quote
Date: 
Message-ID: <xpMvf.38611$i1.36111@news-wrt-01.rdc-nyc.rr.com>
········@fastmail.fm 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.
> 
> Wild Mind
> 

i do not think he meant anything more than:

(list (+ 2 2) '(+ 2 2))
=> (4 (+ 2 2))

kt
From: Stefan Nobis
Subject: Re: newbie question about quote
Date: 
Message-ID: <87bqyowg1l.fsf@snobis.de>
········@fastmail.fm writes:

> Can anyone give a simple example illustrating what Paul Graham means.

(do-something (a b c))

(do-something '(a b c))

In the first case, the function do-something is called with the result
of the call the function a (to which b and c are passed as
parameters). So here the form "(a b c)" is interpreted as code.

In the second example the function do-something is called with one
list containing the symbols a, b and c as parameter -- no extra
function call involved. So here the form "(a b c)" is (thanks to the
quote operator) interpreted as data.

In do-something (in the second case) you still may do a function call
to a (depending on whatever) via something like 

  (apply (car parameter) (cdr parameter))

(given "(defun do-something (parameter) ...)"), so you are able to
take the data and interpret it as code.

In C you have pointers -- this is not really the same, but they are
used for the same idea: There is a bunch of bits and you may interpret
them as code or as data (of course, as you see above, this is much
simpler in Lisp).

-- 
Stefan.
From: Wade Humeniuk
Subject: Re: newbie question about quote
Date: 
Message-ID: <BJRvf.32905$km.10118@edtnps89>
Go Here,

http://www.lisperati.com/casting.html

Wade



········@fastmail.fm 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.
> 
> Wild Mind
> 
From: Joe Marshall
Subject: Re: newbie question about quote
Date: 
Message-ID: <1136669809.603914.52600@z14g2000cwz.googlegroups.com>
········@fastmail.fm 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.