isuy wrote:
> Hello all, Newbie here.
>
> (numberp pi) --> T
> However, (numberp (car '(pi))) --> NIL
>
> Why?
The second is equivalent to (numberp 'pi).
The value the form pi is 3,14159..., which is a number
The value of the form 'pi is pi, which is a symbol.
Paul
isuy wrote:
> Hello all, Newbie here.
>
> (numberp pi) --> T
> However, (numberp (car '(pi))) --> NIL
>
> Why?
>
> Thank you.
You should become acquainted with the Common Lisp HyperSpec:
http://www.lispworks.com/reference/HyperSpec/Front/index.htm
In it you will find the following information about how Lisp evaluates
functions:
http://www.lispworks.com/reference/HyperSpec/Body/03_ababc.htm
Fisrt, the arguments to the function are evaluated. Then the function is
applied to these arguments. In your first example, (numberp pi),
arg PI evaluates to (the floating point number most closely
approximating) pi. Function NUMBERP applied to this number returns T for
true.
However, this evaluation rule may entail recusive evaluation if any of
the arguments themselves are function forms. This is the case in your
second example. In order to call NUMBERP, its arg must be evaluated. But
in order to call CAR, its arg must be evaluated. Well '(pi) evaluates to
the list consisting of the symbol PI (which is not evaluated). The CAR
of this list is the symbol PI. NUMBERP applied to the symbol PI returns
NIL for false, since a symbol is not a number.
See if you can figure out why this is different:
(numberp (car (list pi)))
Hint:
Your example is like this (numberp (car (list 'pi)))
David Sletten
isuy wrote:
> Hello all, Newbie here.
>
> (numberp pi) --> T
> However, (numberp (car '(pi))) --> NIL
>
> Why?
Hint: (numberp (car (list pi))) => t
The Real Problem is that all beginner examples invlove lists which are
generated with this mysterious syntax:
'(a b c)
...so that they do not need to do it with this less obscure syntax:
(list 'a 'b 'c)
You probably already understand this much about symbols:
(let ((a 'b)) (print (list a 'a))) => (b a)
' is short for quote, which means "do not evaluate this bad boy", so 'pi
is just another symbol, not the constant pi (unquoted).
Anyway, because you have been exposed to all these '(a b c) lists, you
have gotten the impression that ' is just how you make a list. It is
not. It is just a handy way to avoid (list 'a 'b 'c), and it works
because QUOTE works on lists as well as atoms.
kt
--
Cells? Cello? Celtik?: http://www.common-lisp.net/project/cells/
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Kenny Tilton <·······@nyc.rr.com> writes:
> isuy wrote:
>
> > Hello all, Newbie here.
> > (numberp pi) --> T
> > However, (numberp (car '(pi))) --> NIL
> > Why?
>
> Hint: (numberp (car (list pi))) => t
>
> The Real Problem is that all beginner examples invlove lists which are
> generated with this mysterious syntax:
>
> '(a b c)
>
> ...so that they do not need to do it with this less obscure syntax:
>
> (list 'a 'b 'c)
>
> You probably already understand this much about symbols:
>
> (let ((a 'b)) (print (list a 'a))) => (b a)
>
> ' is short for quote, which means "do not evaluate this bad boy", so
> 'pi is just another symbol, not the constant pi (unquoted).
>
> Anyway, because you have been exposed to all these '(a b c) lists, you
> have gotten the impression that ' is just how you make a list. It is
> not. It is just a handy way to avoid (list 'a 'b 'c), and it works
> because QUOTE works on lists as well as atoms.
Yes, ' should be avoided in tutorials for newbies. Only use QUOTE.
--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
Pascal Bourguignon <····@mouse-potato.com> wrote:
> Kenny Tilton <·······@nyc.rr.com> writes:
> > Hint: (numberp (car (list pi))) => t
> > The Real Problem is that all beginner examples invlove lists which are
> > generated with this mysterious syntax:
> > '(a b c)
> > ...so that they do not need to do it with this less obscure syntax:
> > (list 'a 'b 'c)
> > You probably already understand this much about symbols:
> > (let ((a 'b)) (print (list a 'a))) => (b a)
> > ' is short for quote, which means "do not evaluate this bad boy", so
> > 'pi is just another symbol, not the constant pi (unquoted).
> > Anyway, because you have been exposed to all these '(a b c) lists, you
> > have gotten the impression that ' is just how you make a list. It is
> > not. It is just a handy way to avoid (list 'a 'b 'c), and it works
> > because QUOTE works on lists as well as atoms.
> Yes, ' should be avoided in tutorials for newbies. Only use QUOTE.
That's not the problem at all. Using ' as an abbreviation for QUOTE
should be quite clear to any intelligent newbie, and certainly was to
me.
The details of the semantics of quote was another story. The fact that
(quote (a b c)) is equivalent to (list (quote a) (quote b) (quote c))
and NOT to (list a b c) was completely *unclear* to me when I first
started learning lisp and was a source of much confusion until I figured
this out.
A question I kept asking myself for the first few dozen hours of my lisp
experience was "How does the evaluator know when to stop evaluating?"
All the examples one sees in tutorials are of course written so that
just what we want evaluated is, and what we don't want evaluated isn't.
We get the general idea that quote is used to forestall evaluation, but
the exact details of what is and isn't forestalled by it are not usually
explained clearly. When we newbies actually start trying to write stuff
ourselves, this results in a lot of errors like the one which prompted
the OP in this thread.
IMO, a statement similar to Kenny's here which makes these semantics
clear, should be in every lisp tutorial at the moment that anyone starts
talking about lists. It would have saved me a lot of confusion, and I'm
clearly not the only one.
Michael
Hi Michael Sullivan,
> The details of the semantics of quote was another story. The fact that
> (quote (a b c)) is equivalent to (list (quote a) (quote b) (quote c))
> and NOT to (list a b c) was completely *unclear* to me when I first
> started learning lisp and was a source of much confusion until I figured
> this out.
Michael, (quote (a b c)) is NOT equivalent to (list (quote a) (quote b)
(quote c)). The first is literal list notation. The second is
destructively modifiable list construction with portable semantics. You
should never destructively modify lists created with the first notation.
For example SBCL takes advantage of being able to coalesce literal lists.
Compile and load this file into SBCL:
(defparameter *literal1* '(a b c))
(defparameter *literal2* '(a b c))
(defparameter *list1* (list 'a 'b 'c))
(defparameter *list2* (list 'a 'b 'c))
You'll find (eq *literal1* *literal2*) is true. The variable names are
pointing to the same object! Remember this is not portable and it only
happened because SBCL has a smarter compiler than say CLISP.
Let's destructively modify *literal1* [replace the second element with
the literal list (b b)]: (setf (second *literal1*) '(b b))
*literal1* now evaluates to (a (b b) c). But so does *literal2*!
Let's try the same with *list1*: (setf (second *list1*) '(b b))
*list1* now evaluates to (a (b b) c). *list2* still evaluates to (a b c).
You will get into a debugging nightmare if you start modifying literal
lists. Common Lispers don't make this mistake often. More common is the
destructive modification of literal strings. Compile and load these lines
into SBCL:
(defparameter *string1* "string")
(defparameter *string2* "string")
(defparameter *string3* (string "string"))
(defparameter *string4* (copy-seq "string"))
In SBCL the first three variables actually point to the same string.
(setf (subseq *string3* 3) "eam")
Now *string1*, *string2* and *string3* all evaluate to "stream".
Note that this only occurred because of the file compilation. If I merely
type/load the source file into SBCL all the (special) variables point to
different strings. Again this could become a debugging nightmare.
Regards,
Adam
isuy <····@sbcglobal.net> writes:
>
>
> Hello all, Newbie here.
>
> (numberp pi) --> T
> However, (numberp (car '(pi))) --> NIL
>
> Why?
Quoting and evaluation.
When you run into these puzzles, you can use the interactive nature of
the Lisp environment to help you:
'pi => PI
pi => 3.14159....
(car '(PI)) => PI
(car (list PI)) => 3.14159...
(car (list 'PI)) => PI
In this case, if you looked at the difference between what you get
typing the two arguments to NUMBERP, you would see that
pi => 3.14159....
(car '(PI)) => PI
If you want more information, then you can also take advantage of the
TYPE-OF function:
(type-of pi) => FLOAT or DOUBLE-FLOAT or LONG-FLOAT (*)
(type-of (car '(pi))) => SYMBOL
(*) it depends on the implementation....
--
Thomas A. Russ, USC/Information Sciences Institute