[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Cons Expression

gengyangcai

10/5/2015 7:39:00 AM


Input --- (cons 'a (cons 'b (cons 'c ' nil)))
Output --- (A B C)

Input --- (cons 'a (cons 'b (cons 'c nil)))
Output --- (A B C)

Why do the 2 expressions give the same output when the one above has an extra (') after the (c) ?
7 Answers

Sebastian Christ

10/5/2015 7:52:00 AM

0


Read:
https://stackoverflow.com/questions/10009508/lisp-is-there-any-difference-between-n...

-- Sebastian

>>>>> On Mon, 5 Oct 2015 00:38:34 -0700 (PDT), CAI GENGYANG <gengyangcai@gmail.com> said:
>
> Input --- (cons 'a (cons 'b (cons 'c ' nil)))
> Output --- (A B C)
>
> Input --- (cons 'a (cons 'b (cons 'c nil)))
> Output --- (A B C)
>
> Why do the 2 expressions give the same output when the one above has an extra (') after the (c) ?

Jussi Piitulainen

10/5/2015 8:03:00 AM

0

CAI GENGYANG writes:

> Input --- (cons 'a (cons 'b (cons 'c ' nil)))
> Output --- (A B C)
>
> Input --- (cons 'a (cons 'b (cons 'c nil)))
> Output --- (A B C)
>
> Why do the 2 expressions give the same output when the one above has
> an extra (') after the (c) ?

It doesn't have a quote after the c. It has a quote before the nil, in
an expression where the 'nil is an expression. You space it funny.

'(cons 'c '32) ==> (cons c (quote 32))
'(cons 'c 32) ==> (cons c 32)

(cons 'c '32) ==> (c . 32)
(cons 'c 32) ==> (c . 32)

'32 ==> 32
32 ==> 32

More or less so.

Consider also (+ 2 (+ 2 0)) and (+ 2 2).

gengyangcai

10/5/2015 9:05:00 AM

0

Ok, so basically nil and 'nil are the same thing ...


On Monday, October 5, 2015 at 4:02:46 PM UTC+8, Jussi Piitulainen wrote:
> CAI GENGYANG writes:
>
> > Input --- (cons 'a (cons 'b (cons 'c ' nil)))
> > Output --- (A B C)
> >
> > Input --- (cons 'a (cons 'b (cons 'c nil)))
> > Output --- (A B C)
> >
> > Why do the 2 expressions give the same output when the one above has
> > an extra (') after the (c) ?
>
> It doesn't have a quote after the c. It has a quote before the nil, in
> an expression where the 'nil is an expression. You space it funny.
>
> '(cons 'c '32) ==> (cons c (quote 32))
> '(cons 'c 32) ==> (cons c 32)
>
> (cons 'c '32) ==> (c . 32)
> (cons 'c 32) ==> (c . 32)
>
> '32 ==> 32
> 32 ==> 32
>
> More or less so.
>
> Consider also (+ 2 (+ 2 0)) and (+ 2 2).

Pascal J. Bourguignon

10/5/2015 10:09:00 AM

0

CAI GENGYANG <gengyangcai@gmail.com> writes:

> Ok, so basically nil and 'nil are the same thing ...

No, not at all.

nil is the representation of a _SYMBOL_, which when READ with the default
*readtable* and the default *package* will be interned to the symbol
COMMON-LISP:NIL (symbol named "NIL" in the package named "COMMON-LISP").

On the other hand, 'nil is the representation of a _LIST_, which when
READ with the default *readtable* and the default *package* will be the
list (COMMON-LISP:QUOTE COMMON-LISP:NIL) containing two symbols, the
symbol named "QUOTE" in the package "COMMON-LISP" and the symbol named
"NIL" in the package "COMMON-LISP".


Now, when you _EVALUATE_ the symbol COMMON-LISP:NIL, you obtain the
symbol COMMON-LISP:NIL, because there is a constant variable
named COMMON-LISP:NIL defined to be bound to the symbol COMMON-LISP:NIL.

On the other hand, when you _EVALUATE_ the list (COMMON-LISP:QUOTE
COMMON-LISP:NIL) you obtain the symbol COMMON-LISP:NIL present in the
argument of this form, because the special operator named
COMMON-LISP:QUOTE does just that, returning the argument unchanged.

Therefore, when you _EVALUATE_ those objects, they both give the same
result, the symbol named COMMON-LISP:NIL.


cl-user> (SETF (READTABLE-CASE *READTABLE*) :PRESERVE)
:PRESERVE
cl-user> (DEFCONSTANT nil 'hello)
nil
cl-user> nil
hello
cl-user> 'nil
nil
cl-user> NIL
NIL
cl-user> 'NIL
NIL
cl-user> (SETF (READTABLE-CASE *READTABLE*) :UPCASE)
:upcase
cl-user>


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Kaz Kylheku

10/5/2015 2:57:00 PM

0

On 2015-10-05, CAI GENGYANG <gengyangcai@gmail.com> wrote:
> Ok, so basically nil and 'nil are the same thing ...

Two different syntactic forms which evaluate to the same thing
*are* the same thing only in languages in which syntax itself
isn't an object.

One such language is conventional mathematics. We have some justification
in asserting that 2x "is the same thing as" x + x.

In Lisp, 'nil and nil evaluate to the same value, but they aren't
the same syntax. That matters in situations in which they are not
evaluated.

One such obvious situation occurs when we pile on just one more quote:

Whereas these evaluate to the same thing

nil -> nil
'nil -> nil

If we quote them, they do not:

'nil -> nil
''nil -> (nil)

In Lisp there are many situations in which nil is specified as an argument,
and isn't evaluated. For instance:

(lambda () ...) ;; function with no arguments

Here, we cannot replace () with '():

(lambda '() ...) ;; function with one invalid argument

This second lambda is actually the syntax (lambda (nil) ...).

On the other hand we are *usually* justified in asserting that nil and NIL
are the same, and nil and () are the same thing.

Of course if these forms are stored in character strings, they are not
the same character strings. However, under usual circumstances, they
denote the same syntax when scanned as input by Lisp.

This isn't always so. The reader can be configured so that nil and NIL
are different symbols. Moreover, in your own package, you can have a
symbol nil which isn't common-lisp:nil, and that symbol doesn't
evaluate to itself, and isn't equivalent to the () notation.

Pascal J. Bourguignon

10/5/2015 3:43:00 PM

0

Kaz Kylheku <kaz@kylheku.com> writes:

> (lambda '() ...) ;; function with one invalid argument

No. Function with two arguments, one of which is invalid.

> This second lambda is actually the syntax (lambda (nil) ...).

No. It is assumedly (COMMON-LISP:LAMBDA (COMMON-LISP:QUOTE COMMON-LISP:NIL) â?¦)
which when macroexpanded becomes
(COMMON-LISP:FUNCTION (COMMON-LISP:LAMBDA (COMMON-LISP:QUOTE COMMON-LISP:NIL) â?¦))
which when evaluated returns a function of two arguments, one named
COMMON-LISP:QUOTE and another named COMMON-LISP:NIL.
Unfortunately, since COMMON-LISP:NIL is defined as a constant variable,
it cannot be bound as a parameter, so an error will occur.

> This isn't always so. The reader can be configured so that nil and NIL
> are different symbols. Moreover, in your own package, you can have a
> symbol nil which isn't common-lisp:nil, and that symbol doesn't
> evaluate to itself, and isn't equivalent to the () notation.

And you can have a reader macro on #\( that reads () just as
your-package:nil.


--
__Pascal Bourguignon__ http://www.informat...
â??The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.� -- Carl Bass CEO Autodesk

Norbert_Paul

10/5/2015 3:46:00 PM

0

Kaz Kylheku wrote:
> On 2015-10-05, CAI GENGYANG<gengyangcai@gmail.com> wrote:
>> Ok, so basically nil and 'nil are the same thing ...
> If we quote them, they do not:
>
> 'nil -> nil
> ''nil -> (nil)

A lession I learned in newsgroups:
You should test even the smallest and most obvious code snipped:

''nil -> 'nil

(At least with sbcl ;) )

Gave me an idea, just for fun :):

(defun q_nil_sum_aux (nil1 nil2)
(if (null nil1)
nil2
(q_nil_sum_aux (cadr nil1) `',nil2)))

(defmacro q_nil_sum (nil1 nil2)
`(q_nil_sum_aux ',nil1 ',nil2))

Tests:

(q_nil_sum '''nil '''''nil) -> ''''''''nil
(q_nil_sum nil '''''nil) -> '''''nil
(q_nil_sum '''''nil nil) -> '''''nil

Norbert