From: ········@fastmail.fm
Subject: Newbie question about quote
Date: 
Message-ID: <1136586605.235796.238360@g43g2000cwa.googlegroups.com>
Hi I'm new to Lisp. Started to read one of Paul Graham's articles where
he says:

"Quote may seem a bit of a foreign concepts 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.

I know that in Lisp code=data is possible. but in what way can quote
operator be used to distinguish between them? Can anyone give a simple
example.

Wild Mind

From: Johan Toki Persson
Subject: Re: Newbie question about quote
Date: 
Message-ID: <1136634550.130372.95450@g43g2000cwa.googlegroups.com>
In a way, think of the quote as an encapsulation.
From: Barry Margolin
Subject: Re: Newbie question about quote
Date: 
Message-ID: <barmar-B5421D.16434907012006@comcast.dca.giganews.com>
In article <························@g43g2000cwa.googlegroups.com>,
 ········@fastmail.fm wrote:

> I know that in Lisp code=data is possible. but in what way can quote
> operator be used to distinguish between them? Can anyone give a simple
> example.

(eval '(list 'list 1 2))
vs.
(eval (list 'list 1 2))

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: John Thingstad
Subject: Re: Newbie question about quote
Date: 
Message-ID: <op.s202wxqcpqzri1@mjolner.upc.no>
On Fri, 06 Jan 2006 23:30:05 +0100, <········@fastmail.fm> wrote:

> Hi I'm new to Lisp. Started to read one of Paul Graham's articles where
> he says:
>
> "Quote may seem a bit of a foreign concepts 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.
>
> I know that in Lisp code=data is possible. but in what way can quote
> operator be used to distinguish between them? Can anyone give a simple
> example.
>
> Wild Mind
>

sure.
(+ 1 2)
=> 3
(quote (+ 1 2))
=> (+ 1 2)

As you see the quote prevented the add operation from being preformed.
In Lisp the first element in a list is a function.
So
(1 2 3)
=> error
Because It can't execute 1.
You want:
(quote (1 2 3))
=> (1 2 3)
or for short:
'(1 2 3)

There is a lot more to be said, but I think I'll stop there
before I confuse you.

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Barry Margolin
Subject: Re: Newbie question about quote
Date: 
Message-ID: <barmar-3FF2B3.17062107012006@comcast.dca.giganews.com>
In article <·················@mjolner.upc.no>,
 "John Thingstad" <··············@chello.no> wrote:

> On Fri, 06 Jan 2006 23:30:05 +0100, <········@fastmail.fm> wrote:
> > I know that in Lisp code=data is possible. but in what way can quote
> > operator be used to distinguish between them? Can anyone give a simple
> > example.
> >
> > Wild Mind
> >
> 
> sure.
> (+ 1 2)
> => 3
> (quote (+ 1 2))
> => (+ 1 2)
> 
> As you see the quote prevented the add operation from being preformed.

But your example isn't related to CL's code=data feature.  It's no 
different from the BASIC statements

PRINT 1 + 2
vs
PRINT "1 + 2"

See my response (possibly in another thread, because the OP decided to 
post his question twice for some reason) where I use EVAL.  EVAL 
provides the link between data and code, and it demonstrates the 
difference.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: John Thingstad
Subject: Re: Newbie question about quote
Date: 
Message-ID: <op.s206m2h3pqzri1@mjolner.upc.no>
On Sat, 07 Jan 2006 23:06:21 +0100, Barry Margolin <······@alum.mit.edu>  
wrote:

>
> See my response (possibly in another thread, because the OP decided to
> post his question twice for some reason) where I use EVAL.  EVAL
> provides the link between data and code, and it demonstrates the
> difference.
>

I saw it but don't quite concur.
To me my code shows why quote is needed.
eval just confuses the issue.

The link between data and the code is the abillity to
write something like.

(defparameter *my-func* '(lambda (a b) (+ a b))
(setf (cadr *my-func*) (append (cadr *my-func*) (cons 'c nil))
(setf (caddp *my-func*) (append (caddr *my-func*) (cons 'c nil))
*my-func*
=> (lambda (a b c) (+ a b c))
(setf *my-func* (eval *my-func*))
(funcall *my-func* 1 2 3)
=> 6

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Barry Margolin
Subject: Re: Newbie question about quote
Date: 
Message-ID: <barmar-C1721A.18002808012006@comcast.dca.giganews.com>
In article <·················@mjolner.upc.no>,
 "John Thingstad" <··············@chello.no> wrote:

> On Sat, 07 Jan 2006 23:06:21 +0100, Barry Margolin <······@alum.mit.edu>  
> wrote:
> 
> >
> > See my response (possibly in another thread, because the OP decided to
> > post his question twice for some reason) where I use EVAL.  EVAL
> > provides the link between data and code, and it demonstrates the
> > difference.
> >
> 
> I saw it but don't quite concur.
> To me my code shows why quote is needed.

True, but that wasn't what the OP asked.  Like I showed, BASIC also 
requires quoting to prevent evaluation, but it doesn't have code=data.

> eval just confuses the issue.
> 
> The link between data and the code is the abillity to
> write something like.

And notice that you have to quote because later on you're going to use 
EVAL.

> 
> (defparameter *my-func* '(lambda (a b) (+ a b))
> (setf (cadr *my-func*) (append (cadr *my-func*) (cons 'c nil))
> (setf (caddp *my-func*) (append (caddr *my-func*) (cons 'c nil))
> *my-func*
> => (lambda (a b c) (+ a b c))
> (setf *my-func* (eval *my-func*))
> (funcall *my-func* 1 2 3)
> => 6

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: John Thingstad
Subject: Re: Newbie question about quote
Date: 
Message-ID: <op.s22130olpqzri1@mjolner.upc.no>
On Mon, 09 Jan 2006 00:00:28 +0100, Barry Margolin <······@alum.mit.edu>  
wrote:

Yes I notice!
And it kills me.
I know there is a better way!
ok. please show me..

>>
>> (defparameter *my-func* '(lambda (a b) (+ a b))
>> (setf (cadr *my-func*) (append (cadr *my-func*) (cons 'c nil))
>> (setf (caddp *my-func*) (append (caddr *my-func*) (cons 'c nil))
>> *my-func*
>> => (lambda (a b c) (+ a b c))
>> (setf *my-func* (eval *my-func*))
>> (funcall *my-func* 1 2 3)
>> => 6
>



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: Pascal Bourguignon
Subject: Re: Newbie question about quote
Date: 
Message-ID: <87r77iw9pd.fsf@thalassa.informatimago.com>
"John Thingstad" <··············@chello.no> writes:

> On Mon, 09 Jan 2006 00:00:28 +0100, Barry Margolin
> <······@alum.mit.edu>  wrote:
>
> Yes I notice!
> And it kills me.
> I know there is a better way!
> ok. please show me..
>
>>> (defparameter *my-func* '(lambda (a b) (+ a b))

First, you shall not modify quoted data:

(defparameter *my-func* (copy-tree '(lambda (a b) (+ a b))))


>>> (setf (cadr *my-func*) (append (cadr *my-func*) (cons 'c nil))
>>> (setf (caddp *my-func*) (append (caddr *my-func*) (cons 'c nil))

Next, now that you have a unique copy of the tree, you can use nconc:

(setf (cadr  *my-func*) (nconc (cadr  *my-func*) (cons 'c nil))
(setf (caddr *my-func*) (nconc (caddr *my-func*) (cons 'c nil))


>>> *my-func*
>>> => (lambda (a b c) (+ a b c))
>>> (setf *my-func* (eval *my-func*))
>>> (funcall *my-func* 1 2 3)
>>> => 6

Finally, you can embed the transformation into a macro and get a free quote+eval:

(defmacro appendf (item place)
  (warn "Quick and dirty; should implement a defsetf APPENDF!")
  `(setf ,place (nconc ,place (cons ,item nil))))

(defmacro one-more-argument (lambda-form)
   (let ((lambda-form (copy-tree lambda-form))
         (new-arg (gensym)))
      (appendf new-arg (cadr  lambda-form))
      (appendf new-arg (caddr lambda-form))
      lambda-form))


[16]> (macroexpand-1 '(one-more-argument (lambda (a b) (+ a b))))
(LAMBDA (A B #:G4321) (+ A B #:G4321)) ;
T
[17]> (one-more-argument (lambda (a b) (+ a b)))
#<FUNCTION :LAMBDA (A B #:G4322) (+ A B #:G4322)>
[18]> (funcall (one-more-argument (lambda (a b) (+ a b))) 1 2 3)
6
[19]> 



(But otherwise, there's nothing wrong in quoting and manipulating
sexps to eventually evaluate them.)

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

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: John Thingstad
Subject: Re: Newbie question about quote
Date: 
Message-ID: <op.s23nxexppqzri1@mjolner.upc.no>
On Mon, 09 Jan 2006 01:01:34 +0100, Pascal Bourguignon  
<····@mouse-potato.com> wrote:

The nconc I thought of myself.
You missed the most obvious optimation though..
(setf *my-func* (compile *my-func*))


> "John Thingstad" <··············@chello.no> writes:
>
>> On Mon, 09 Jan 2006 00:00:28 +0100, Barry Margolin
>> <······@alum.mit.edu>  wrote:
>>
>> Yes I notice!
>> And it kills me.
>> I know there is a better way!
>> ok. please show me..
>>
>>>> (defparameter *my-func* '(lambda (a b) (+ a b))
>
> First, you shall not modify quoted data:
>
> (defparameter *my-func* (copy-tree '(lambda (a b) (+ a b))))
>
>
>>>> (setf (cadr *my-func*) (append (cadr *my-func*) (cons 'c nil))
>>>> (setf (caddp *my-func*) (append (caddr *my-func*) (cons 'c nil))
>
> Next, now that you have a unique copy of the tree, you can use nconc:
>
> (setf (cadr  *my-func*) (nconc (cadr  *my-func*) (cons 'c nil))
> (setf (caddr *my-func*) (nconc (caddr *my-func*) (cons 'c nil))
>
>
>>>> *my-func*
>>>> => (lambda (a b c) (+ a b c))
>>>> (setf *my-func* (eval *my-func*))
>>>> (funcall *my-func* 1 2 3)
>>>> => 6
>
> Finally, you can embed the transformation into a macro and get a free  
> quote+eval:
>
> (defmacro appendf (item place)
>   (warn "Quick and dirty; should implement a defsetf APPENDF!")
>   `(setf ,place (nconc ,place (cons ,item nil))))
>
> (defmacro one-more-argument (lambda-form)
>    (let ((lambda-form (copy-tree lambda-form))
>          (new-arg (gensym)))
>       (appendf new-arg (cadr  lambda-form))
>       (appendf new-arg (caddr lambda-form))
>       lambda-form))
>
>
> [16]> (macroexpand-1 '(one-more-argument (lambda (a b) (+ a b))))
> (LAMBDA (A B #:G4321) (+ A B #:G4321)) ;
> T
> [17]> (one-more-argument (lambda (a b) (+ a b)))
> #<FUNCTION :LAMBDA (A B #:G4322) (+ A B #:G4322)>
> [18]> (funcall (one-more-argument (lambda (a b) (+ a b))) 1 2 3)
> 6
> [19]>
>
>
>
> (But otherwise, there's nothing wrong in quoting and manipulating
> sexps to eventually evaluate them.)
>



-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: John Thingstad
Subject: Re: Newbie question about quote
Date: 
Message-ID: <op.s23n6dwupqzri1@mjolner.upc.no>
On Mon, 09 Jan 2006 08:18:40 +0100, John Thingstad  
<··············@chello.no> wrote:

ah! SBCL man... (Only in Corman and SBCL would it make no difference..)

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
From: LuisGLopez
Subject: Re: Newbie question about quote
Date: 
Message-ID: <1136657911.016556.282960@g47g2000cwa.googlegroups.com>
········@fastmail.fm ha escrito:

> Hi I'm new to Lisp. Started to read one of Paul Graham's articles where
> he says:
>
> "Quote may seem a bit of a foreign concepts 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.
>
> I know that in Lisp code=data is possible. but in what way can quote
> operator be used to distinguish between them? Can anyone give a simple
> example.
>
> Wild Mind

Hi!!! :)

May be this can answer your question:

CL-USER> (defun please-do (expresion)
	   (format t "Ok, I'll do it for you. ~a yields ~a.~%" expresion (eval
expresion)))
PLEASE-DO
CL-USER> (please-do '(+ 2 3))
Ok, I'll do it for you. (+ 2 3) yields 5.
NIL

I'm newbie too, so maybe it can be donde in a better way. Like that, it
just works :)
From: netytan
Subject: Re: Newbie question about quote
Date: 
Message-ID: <1136653615.559231.12500@z14g2000cwz.googlegroups.com>
I'm pretty new myself but I believe he means that quote can be used to
stop evaluation of a section of code and return it as is; hence the
quoted code is now data because it isn't executed.

Mark.