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
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
>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/
"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