From: Gene Ash
Subject: Eval function
Date: 
Message-ID: <1163407470.167637.15190@f16g2000cwb.googlegroups.com>
Hi,

I am a newbie to lisp and am going through Touretzky's book. I came
across the following exercise about Eval in chapter 3.

(eval (list 'eval nil))

As the List (list 'eval nil) is a valid one, I thought that this should
evaluate to T. But this evaluates to NIL. Can someone explain as to why
this evaluates to NIL instead of T?

Thanks,
Gene

From: Prabu
Subject: Re: Eval function
Date: 
Message-ID: <1163412091.391710.304350@b28g2000cwb.googlegroups.com>
Gene Ash wrote:
> Hi,
>
> I am a newbie to lisp and am going through Touretzky's book. I came
> across the following exercise about Eval in chapter 3.
>
> (eval (list 'eval nil))
>
> As the List (list 'eval nil) is a valid one, I thought that this should
> evaluate to T. But this evaluates to NIL. Can someone explain as to why
> this evaluates to NIL instead of T?
>
> Thanks,
> Gene

(eval (list 'eval nil))

Trace it out

> (eval (list 'eval nil))

> (list 'eval nil)
   gives
   (eval nil)

> (eval nil)
   gives
   nil

> (eval nil)
   gives
   nil

>nil
  prints
  nil


Life is Nil

Prabu
From: Gene Ash
Subject: Re: Eval function
Date: 
Message-ID: <1163414295.279836.203030@m7g2000cwm.googlegroups.com>
Hi Barry/ Prabu,

Thanks to you for explaining the concept in such a lucid manner.

Cheers,
Gene

Prabu wrote:
> Gene Ash wrote:
> > Hi,
> >
> > I am a newbie to lisp and am going through Touretzky's book. I came
> > across the following exercise about Eval in chapter 3.
> >
> > (eval (list 'eval nil))
> >
> > As the List (list 'eval nil) is a valid one, I thought that this should
> > evaluate to T. But this evaluates to NIL. Can someone explain as to why
> > this evaluates to NIL instead of T?
> >
> > Thanks,
> > Gene
>
> (eval (list 'eval nil))
>
> Trace it out
>
> > (eval (list 'eval nil))
>
> > (list 'eval nil)
>    gives
>    (eval nil)
>
> > (eval nil)
>    gives
>    nil
>
> > (eval nil)
>    gives
>    nil
> 
> >nil
>   prints
>   nil
> 
> 
> Life is Nil
> 
> Prabu
From: ·················@gmail.com
Subject: Re: Eval function
Date: 
Message-ID: <1163418531.579403.102270@m7g2000cwm.googlegroups.com>
> Trace it out
>
> > (eval (list 'eval nil))
> > (list 'eval nil)   gives
>    (eval nil)
>

I think you are a bit wrong in this place, the outer eval evaluates
'(eval nil) and returns the nil returned by the inner eval. And you are
writing it in such way that the value returned by (eval nil) ir
reevaluated once more yielding nil:

> > (eval nil)   gives
>    nil
>
> > (eval nil)   gives
>    nil
>
> >nil  prints
>   nil

with something non nil but let's say '(+ 2 3) in there you'd get

It's more like:

* (eval (list 'eval nil))
  0: (LIST EVAL NIL)
  0: LIST returned (EVAL NIL)
  0: (EVAL (EVAL NIL)) -> the outer eval evaluates (eval nil)
    1: (EVAL NIL) -> inner eval evaluates nil
    1: EVAL returned NIL -> inner eval returns nil
  0: EVAL returned NIL -> outer eval returns the value returned by the
inner eval

Ignas
From: Pascal Bourguignon
Subject: Re: Eval function
Date: 
Message-ID: <87wt5zidep.fsf@thalassa.informatimago.com>
··················@gmail.com" <·················@gmail.com> writes:

>> Trace it out
>>
>> > (eval (list 'eval nil))
>> > (list 'eval nil)   gives
>>    (eval nil)
>>
>
> I think you are a bit wrong in this place, the outer eval evaluates
> '(eval nil) and returns the nil returned by the inner eval. And you are
> writing it in such way that the value returned by (eval nil) ir
> reevaluated once more yielding nil:

This is a matter of convention.

I find it clearer to  say that the form (list 'eval nil)
evaluates to (eval nil)
^^^^^^^^^^^^
than to say that it evaluates to the same result as '(eval nil)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
given that the underlined parts are usually elided.


More over, if you can try at the REPL:


(equal (list 'eval nil) (read)) RET
'(eval nil) RET


(equal (list 'eval nil) (read)) RET
(eval nil) RET


Which one gives T?
This is the one I'd write.

-- 
__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: Barry Margolin
Subject: Re: Eval function
Date: 
Message-ID: <barmar-E553E8.04563313112006@comcast.dca.giganews.com>
In article <·······················@f16g2000cwb.googlegroups.com>,
 "Gene Ash" <········@gmail.com> wrote:

> Hi,
> 
> I am a newbie to lisp and am going through Touretzky's book. I came
> across the following exercise about Eval in chapter 3.
> 
> (eval (list 'eval nil))
> 
> As the List (list 'eval nil) is a valid one, I thought that this should
> evaluate to T. But this evaluates to NIL. Can someone explain as to why
> this evaluates to NIL instead of T?

The way you're describing your expectation, it sounds like you think 
that EVAL tells you whether something is a valid expression.  But that's 
not what it does -- it executes the expression and returns its result.  
For instance,

(eval (list '+ 2 3))

returns 5 because it executes

(+ 2 3)

-- 
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: Frank Buss
Subject: Re: Eval function
Date: 
Message-ID: <u8voxse8o11g$.a28ew5pj1lvj.dlg@40tude.net>
Gene Ash wrote:

> (eval (list 'eval nil))
> 
> As the List (list 'eval nil) is a valid one, I thought that this should
> evaluate to T. But this evaluates to NIL. Can someone explain as to why
> this evaluates to NIL instead of T?

The outer eval evals the eval which is created by the form (list 'eval
nil).

BTW: usually eval is very seldom used in Lisp programs, if you don't write
your own REPL.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Gene Ash
Subject: Re: Eval function
Date: 
Message-ID: <1163411903.872440.116430@i42g2000cwa.googlegroups.com>
Thank you Frank. Your explanation has cleared my doubt.

Cheers,
Gene

Frank Buss wrote:
> Gene Ash wrote:
>
> > (eval (list 'eval nil))
> >
> > As the List (list 'eval nil) is a valid one, I thought that this should
> > evaluate to T. But this evaluates to NIL. Can someone explain as to why
> > this evaluates to NIL instead of T?
>
> The outer eval evals the eval which is created by the form (list 'eval
> nil).
>
> BTW: usually eval is very seldom used in Lisp programs, if you don't write
> your own REPL.
>
> --
> Frank Buss, ··@frank-buss.de
> http://www.frank-buss.de, http://www.it4-systems.de
From: ··········@hotmail.com
Subject: Re: Eval function
Date: 
Message-ID: <1163525390.954069.66340@e3g2000cwe.googlegroups.com>
> Hi,
>
> I am a newbie to lisp and am going through Touretzky's book. I came
> across the following exercise about Eval in chapter 3.
>
> (eval (list 'eval nil))
>
> As the List (list 'eval nil) is a valid one, I thought that this should
> evaluate to T. But this evaluates to NIL. Can someone explain as to why
> this evaluates to NIL instead of T?
>
> Thanks,
> Gene

Hi,
try to step your expression:

   (eval (list 'eval nil))

through this imaginary lisp interpretator:

1 - read in an expression
    recursively go through the whole expression and
    just look at all sub expressions. If symbols is found, bind them,
    but keep them unbound ( they exist as symbols, but no slots in the
    symbol has values, not even nil ).

2 - evaluate expression
    2.1 if expression is an atom,
        2.1.1 if expression is an variable:
              If variable has no package prefix, assume the package
              name as given in *package*.
              Now look down the lexical environment stack for any
              binding of this variable.
              If found: return value binded to variable.
              Else return variable-not-bound-error.
        2.1.2 if expression is an atom but not an variable return it
as-is
              nil => nil, t => t, 1 => 1, 'hi => HI etc..
    2.2 if expression is an list then treat it as an function call.
        2.2.1 foreach arguments (second item downto last item in list)
              recursively call our interpreter, and
              return back here with the result from the repl.
              To ease your memory, make a copy of the expression and
              replace each argument with the result as you advance.
        2.2.2 Now that all arguments has been evaluated from left to
right,
              we can call the function that has function name given in
              the first item of the list.


3 - print expression if top-level
    Print the returned/end-result from the previous step.

4 - goto step 1 if top-level, else return value from step 2.

here is how you would play this "game":

scratchpad:               step-stack:  notes:
  (eval (list 'eval nil)) 1            read in expression
        (list 'eval nil)  2.2.1        copy of the arguments, for
clarity
        (list 'eval nil)  2.2.1,1      here we call our repl
recursively
                                       starting from step 1, read in
expression
              'eval nil   2.2.1,2.2.1  copy of arguments,
              EVAL NIL    2.2.1,2.2.1  replace arguments from left to
right
        (list EVAL NIL )  2.2.1,2.2.2  return and do function call of
name #'LIST
        (EVAL NIL)        2.2.1,4      result of evaluation unquoted
  (eval (EVAL NIL)     )  2.2.2        all arguments evaled, do (outer)
function call
                                       [1] [2].
  (eval (EVAL NIL)     )  2.2.2,1      read in expression
        (EVAL NIL)        2.2.2,2.2.1  .. ok, its a list, lets do
function call
                                       copy of arguments
        (EVAL NIL)        2.2.2,2.2.1  argument is a list, lets do
funcall..
        (EVAL NIL)        2.2.2,2.2.1,1 read in expression
              NIL         2.2.2,2.2.1,2.2.1 copy of args
              NIL         2.2.2,2.2.1,2.2.2 evaulate args
        (eval NIL)        2.2.2,2.2.1,2.2.2 all args evaled do funcall
        NIL               2.2.2,2.2.1,4     return result
  (eval NIL            )  2.2.2,2.2.2       all args evaled do funcall
        NIL               2.2.2,4           return result
        NIL               2.2.2             all args evaled do funcall
        NIL               4                 return result


[1] advanced: a function reference: symbol-function slot in symbol
named by the variable eval that reader established.

[2] the result of the evaluation looks like it must be evaluate again
again
    but that is because we write in the scratchpad unquoted, and in the
real repl you must quote it.
From: Prabu
Subject: Re: Eval function
Date: 
Message-ID: <1163576807.470366.160640@m73g2000cwd.googlegroups.com>
··········@hotmail.com wrote:

> > Hi,
> >
> > I am a newbie to lisp and am going through Touretzky's book. I came
> > across the following exercise about Eval in chapter 3.
> >
> > (eval (list 'eval nil))
> >
> > As the List (list 'eval nil) is a valid one, I thought that this should
> > evaluate to T. But this evaluates to NIL. Can someone explain as to why
> > this evaluates to NIL instead of T?
> >
> > Thanks,
> > Gene
>
> Hi,
> try to step your expression:
>
>    (eval (list 'eval nil))
>
> through this imaginary lisp interpretator:
>
> 1 - read in an expression
>     recursively go through the whole expression and
>     just look at all sub expressions. If symbols is found, bind them,
>     but keep them unbound ( they exist as symbols, but no slots in the
>     symbol has values, not even nil ).
>
> 2 - evaluate expression
>     2.1 if expression is an atom,
>         2.1.1 if expression is an variable:
>               If variable has no package prefix, assume the package
>               name as given in *package*.
>               Now look down the lexical environment stack for any
>               binding of this variable.
>               If found: return value binded to variable.
>               Else return variable-not-bound-error.
>         2.1.2 if expression is an atom but not an variable return it
> as-is
>               nil => nil, t => t, 1 => 1, 'hi => HI etc..
>     2.2 if expression is an list then treat it as an function call.
>         2.2.1 foreach arguments (second item downto last item in list)
>               recursively call our interpreter, and
>               return back here with the result from the repl.
>               To ease your memory, make a copy of the expression and
>               replace each argument with the result as you advance.
>         2.2.2 Now that all arguments has been evaluated from left to
> right,
>               we can call the function that has function name given in
>               the first item of the list.
>
>
> 3 - print expression if top-level
>     Print the returned/end-result from the previous step.
>
> 4 - goto step 1 if top-level, else return value from step 2.
>
> here is how you would play this "game":
>
> scratchpad:               step-stack:  notes:
>   (eval (list 'eval nil)) 1            read in expression
>         (list 'eval nil)  2.2.1        copy of the arguments, for
> clarity
>         (list 'eval nil)  2.2.1,1      here we call our repl
> recursively
>                                        starting from step 1, read in
> expression
>               'eval nil   2.2.1,2.2.1  copy of arguments,
>               EVAL NIL    2.2.1,2.2.1  replace arguments from left to
> right
>         (list EVAL NIL )  2.2.1,2.2.2  return and do function call of
> name #'LIST
>         (EVAL NIL)        2.2.1,4      result of evaluation unquoted
>   (eval (EVAL NIL)     )  2.2.2        all arguments evaled, do (outer)
> function call
>                                        [1] [2].
>   (eval (EVAL NIL)     )  2.2.2,1      read in expression
>         (EVAL NIL)        2.2.2,2.2.1  .. ok, its a list, lets do
> function call
>                                        copy of arguments
>         (EVAL NIL)        2.2.2,2.2.1  argument is a list, lets do
> funcall..
>         (EVAL NIL)        2.2.2,2.2.1,1 read in expression
>               NIL         2.2.2,2.2.1,2.2.1 copy of args
>               NIL         2.2.2,2.2.1,2.2.2 evaulate args
>         (eval NIL)        2.2.2,2.2.1,2.2.2 all args evaled do funcall
>         NIL               2.2.2,2.2.1,4     return result
>   (eval NIL            )  2.2.2,2.2.2       all args evaled do funcall
>         NIL               2.2.2,4           return result
>         NIL               2.2.2             all args evaled do funcall
>         NIL               4                 return result
>
>
> [1] advanced: a function reference: symbol-function slot in symbol
> named by the variable eval that reader established.
>
> [2] the result of the evaluation looks like it must be evaluate again
> again
>     but that is because we write in the scratchpad unquoted, and in the
> real repl you must quote it.

Applause