From: ·······@gmail.com
Subject: Evaluation problem (Newbie)
Date: 
Message-ID: <1183639032.376189.11420@e16g2000pri.googlegroups.com>
I've just started to learn lisp and got the following problem:
If I define a function like this (defun f (x) (cons x ())) , and use
it like this (f   'x).
first of all , the interpreter evaluates the 'x and gets x, second ,
it subsitute x in to (cons x ()).
the next step is what confuse me , (cons x ()) is a statement , in
order to evaluate it , x should be evaluated first ,
but x is not preceded by the char #\` as in (f   'x) , if x is
evaluated we should get a error message.
But when I try this code in clisp, it's ok, works exactly as what I
would expected . Can anyone tell me what's going on about this? Thanks
a lot !

From: ·······@gmail.com
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <1183639351.486657.205240@k79g2000hse.googlegroups.com>
On Jul 5, 8:37 am, ········@gmail.com" <·······@gmail.com> wrote:
> I've just started to learn lisp and got the following problem:
> If I define a function like this (defun f (x) (cons x ())) , and use
> it like this (f   'x).
> first of all , the interpreter evaluates the 'x and gets x, second ,
> it subsitute x in to (cons x ()).
> the next step is what confuse me , (cons x ()) is a statement , in
> order to evaluate it , x should be evaluated first ,
> but x is not preceded by the char #\` as in (f   'x) , if x is
> evaluated we should get a error message.
> But when I try this code in clisp, it's ok, works exactly as what I
> would expected . Can anyone tell me what's going on about this? Thanks
> a lot !

in (f 'x), 'x is evaluated to the symbol x. then in (cons x ()), x is
evaluated to the binding defined by the function's argument list. so x
is substituted for the literal symbol x
From: ·······@gmail.com
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <1183641441.319927.143500@i38g2000prf.googlegroups.com>
On 7 5 ,   8 42 , ·······@gmail.com wrote:
> On Jul 5, 8:37 am, ········@gmail.com" <·······@gmail.com> wrote:
>
> > I've just started to learn lisp and got the following problem:
> > If I define a function like this (defun f (x) (cons x ())) , and use
> > it like this (f   'x).
> > first of all , the interpreter evaluates the 'x and gets x, second ,
> > it subsitute x in to (cons x ()).
> > the next step is what confuse me , (cons x ()) is a statement , in
> > order to evaluate it , x should be evaluated first ,
> > but x is not preceded by the char #\` as in (f   'x) , if x is
> > evaluated we should get a error message.
> > But when I try this code in clisp, it's ok, works exactly as what I
> > would expected . Can anyone tell me what's going on about this? Thanks
> > a lot !
>
> in (f 'x), 'x is evaluated to the symbol x. then in (cons x ()), x is
> evaluated to the binding defined by the function's argument list. so x
> is substituted for the literal symbol x

Thank you for your reply !
So according to your post , if we define a function like this : (defun
f (x1 x2 x3 ...  xn) somecode) , and then run (f t1 t2 ... tn) , first
the interpreter evaluates t1 t2 ... and tn ,then it turn to "somecode"
and try to evaluate it , once it encounters say xi , it uses the value
from ti and DO NOT EVALUATE IT AGAIN , that's right?
From: ·······@gmail.com
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <1183642104.193240.290410@m36g2000hse.googlegroups.com>
On Jul 5, 9:17 am, ········@gmail.com" <·······@gmail.com> wrote:
> if we define a function like this : (defun
> f (x1 x2 x3 ...  xn) somecode) , and then run (f t1 t2 ... tn) , first
> the interpreter evaluates t1 t2 ... and tn ,then it turn to "somecode"
> and try to evaluate it , once it encounters say xi , it uses the value
> from ti and DO NOT EVALUATE IT AGAIN , that's right?

that's right. easy to test and verify too!
From: Rob Warnock
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <eKidnbB4Pt7ZXBDbnZ2dnUVZ_qemnZ2d@speakeasy.net>
·······@gmail.com <·······@gmail.com> wrote:
+---------------
| Thank you for your reply !
| So according to your post , if we define a function like this : (defun
| f (x1 x2 x3 ...  xn) somecode) , and then run (f t1 t2 ... tn) , first
| the interpreter evaluates t1 t2 ... and tn ,then it turn to "somecode"
| and try to evaluate it , once it encounters say xi , it uses the value
| from ti and DO NOT EVALUATE IT AGAIN , that's right?
+---------------

Correct. In the language of subroutine call protocols, this is
known as "call-by-value", and Common Lisp, Scheme, and most other
widely-used languages these days (including C, C++, and Java)
are call-by-value languages.[1]  In call-by-value, the "actual"
arguments to a function call [your "t1", "t2", etc.] are evaluated
exactly once, in the caller's environment, and only the resulting
values of those evaluations are passed to the called routine. Nothing
of the form or shape [e.g., names, arithmetic expressions] of those
arguments is passed to the callee, only the evaluated values.

In the called routine, the variables named by the "formal"
arguments [your "x1", "x2", etc.] are bound, as if by LET,
to the values passed from the caller.[2]  Later assignments
to those variable in the callee (with SETF, etc.) of different
values to the formal argument variables may change their values
at that time, but such changes have no effect on the caller's
environment. Call-by-value is entirely one-way [except for the
callee's return value, of course].

There *are* other styles of subroutine call protocols, including
"call-by-reference" [e.g., Fortran], and "call-by-name" [e.g.,
Algol-60]. The semantics of call-by-name are especially weird,
but that's another story...


-Rob

[1] Common Lisp macros are another story entirely.

[2] Details of the processing of &OPTIONAL, &KEY, and &REST
    arguments are a further complication, but even there,
    *if* any specific argument is included by the caller,
    the subroutine call protocol still obeys call-by-value
    semantics w.r.t that argument.

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Thomas A. Russ
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <ymi644ypslm.fsf@sevak.isi.edu>
········@gmail.com" <·······@gmail.com> writes:

> I've just started to learn lisp and got the following problem:
> If I define a function like this (defun f (x) (cons x ())) , and use
> it like this (f   'x).

Try thinking about (f 'y) instead and see if that helps clear things up.

> first of all , the interpreter evaluates the 'x and gets x, second ,

and then binds the variable X to the symbol X.

> it subsitute x in to (cons x ()).

it substitutes the value bound to the variable X, which confusingly
happens to be the symbol X into the form.

> the next step is what confuse me , (cons x ()) is a statement , in
> order to evaluate it , x should be evaluated first ,
> but x is not preceded by the char #\` as in (f   'x) , if x is
> evaluated we should get a error message.
> But when I try this code in clisp, it's ok, works exactly as what I
> would expected . Can anyone tell me what's going on about this? Thanks
> a lot !


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal Bourguignon
Subject: Re: Evaluation problem (Newbie)
Date: 
Message-ID: <87644zuec0.fsf@thalassa.lan.informatimago.com>
········@gmail.com" <·······@gmail.com> writes:

> I've just started to learn lisp and got the following problem:
> If I define a function like this (defun f (x) (cons x ())) , and use
> it like this (f   'x).
> first of all , the interpreter evaluates the 'x and gets x, second ,
> it subsitute x in to (cons x ()).
> the next step is what confuse me , (cons x ()) is a statement , in
> order to evaluate it , x should be evaluated first ,
> but x is not preceded by the char #\` as in (f   'x) , if x is
> evaluated we should get a error message.
> But when I try this code in clisp, it's ok, works exactly as what I
> would expected . Can anyone tell me what's going on about this? Thanks
> a lot !


When you call a function, the arguments are evaluated and bound to the
parameters.

Notice the equivalence between LET and LAMBDA:


(let ((a (+ 1 2))
      (b (* 3 4)))
   (+ a b))

===

((lambda (a b) (+ a b))  (+ 1 2) (* 3 4))

===

(defun f (a b) (+ a b))
(f (+ 1 2) (* 3 4))


Therefore,

(defun f (x) (cons x nil))
(f 'x)

===

((lambda (x) (cons x nil)) 'x)

===

(let ((x 'x))
   (cons x nil))

So you see clearly that the variable named by the symbol X is bound to
the result of evaluating (QUOTE X),  which is the symbol X itself, and
therefore when evaluating (CONS X NIL), X evaluates to the value bound
to it, that is, the symbol X, and CONS returns (X . NIL) === (X).



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

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.