From: Uwe Mayer
Subject: newbe: ''(1 . 2) vs. '(1 2)
Date: 
Message-ID: <dbtrea$7df$1@news2.rz.uni-karlsruhe.de>
Hi,

I'm new to Lisp and am experimenting with some of the functions when I come
across this:

(length '(1 2))
; 2

(length ''(1 . 2))
; 2

Why does the following return a (wrong-type-argument listp 2)?
(length '(1 . 2))

What does "(1 . 2)" mean exactly?
I came to that expression when I used cons the following way:

(cons 1 2)
% (1 . 2)

I know cons creates a list by joining head +tail and that  "2" is an atom,
but I am confused as that (1 . 2) has two elements. 
Also comparing "(1 2)" with "(1 . 2)" always returns nil, why?

Thanks in advance,
Ciao
Uwe 
        

From: Pietro Campesato
Subject: Re: newbe: ''(1 . 2) vs. '(1 2)
Date: 
Message-ID: <1122136945.207319.144020@g43g2000cwa.googlegroups.com>
You should know the following:
'(1 2) is the same as (quote (1 2)) and, when evaluated (1 2)
''(1 . 2) is (quote (quote (1 2)): when eval'd gives (quote (1 . 2))

You can think of length this way: it counts all top-level elements of
a _proper_ list: in the first case these elements are 1 and 2; in the
second, they are the symbol quote and the _dotted_ list (1 . 2)...

The problem with (length '(1 . 2)) is that length wants a _proper_
list,
not a dotted one...

I hope I've been clear enough (else, there's plenty of knowledgeable
ppl here :)

Ciao
From: Pietro Campesato
Subject: Re: newbe: ''(1 . 2) vs. '(1 2)
Date: 
Message-ID: <1122137086.171282.131440@o13g2000cwo.googlegroups.com>
Sorry, ''(1 . 2) is (quote (quote (1 . 2)))
From: Eric Lavigne
Subject: Re: newbe: ''(1 . 2) vs. '(1 2)
Date: 
Message-ID: <1122140804.702396.103830@g49g2000cwa.googlegroups.com>
>Why does the following return a (wrong-type-argument listp 2)?
>(length '(1 . 2))

length expects a list. (1 . 2) is not a list.

>What does "(1 . 2)" mean exactly?
>I came to that expression when I used cons the following way:
>
>(cons 1 2)
>% (1 . 2)

a cons contains two values. (cons 1 2) creates a cons with the values 1
and 2.
(1 . 2) is a cons with values 1 and 2.

>I know cons creates a list by joining head +tail
A list is a cons inside of a cons inside of a cons..... in which the
innermost cons has nil for the second value. Example:
the list (1 2 3 4) could be represented like this:
(cons 1 (cons 2 (cons 3 (cons 4 nil))))
or
(1 . (2 . (3 . (4 . nil))))

>(length ''(1 . 2))
>; 2

As others have already said, this is interpreted as (length '(quote (1
. 2))).
The two items in the list are "quote" and "(1 . 2)"
When you give two quotes, the first quote prevents evaluation of the
rest of the expression. Therefore the second quote never gets evaluated
and remains part of the argument.

I recommend reading a beginner's book on Lisp to learn these things
quickly. One good option is Practical Common Lisp:
www.gigamonkeys.com/book/
From: Kent M Pitman
Subject: Re: newbe: ''(1 . 2) vs. '(1 2)
Date: 
Message-ID: <ufyu52y2x.fsf@nhplace.com>
"Eric Lavigne" <············@gmail.com> writes:

> >Why does the following return a (wrong-type-argument listp 2)?
> >(length '(1 . 2))
> 
> length expects a list. (1 . 2) is not a list.

length expects a proper list.

(1 . 2) is a list
http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_l.htm#list

(1 . 2) is not a proper list.
http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_p.htm#proper_list

> >What does "(1 . 2)" mean exactly?
> >I came to that expression when I used cons the following way:
> >
> >(cons 1 2)
> >% (1 . 2)
>
> a cons contains two values. (cons 1 2) creates a cons with the values 1
> and 2.
> (1 . 2) is a cons with values 1 and 2.

http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#dotted_pair
http://www.lispworks.com/documentation/HyperSpec/Body/26_glo_d.htm#dotted_list