tags:

views:

170

answers:

5

In SICP exercise 2.26, this Scheme code is given:

(define x (list 1 2 3))
(define y (list 4 5 6))

Then this cons call is given:

(cons x y)

I expected a pair of lists would result, ((1 2 3) (4 5 6)) but the interpreter gives, ((1 2 3) 4 5 6) ...a list with 4 elements, the first being a list. Why is y treated differently? I've tried looking up other SICP answers for an explanation, but couldn't find something satisfactory. So could any Scheme/Lisp experts please shed some light on this aspect of cons? Thanks in advance for any insight.

+4  A: 

cons uses the first argument as head of the list, and the second as tail.

You give it a first list (1 2 3), which will constitute the head of the resulting list and a second list (4 5 6), to be used as tail of the list. Thus, you end with ((1 2 3) 4 5 6).

Thing of lists as left-to-right combs, ending with empty list (represented as o here), and see how they combine.

 X=      Y=
 /\      /\
1 /\  + 4 /\    
 2 /\    5 /\  
  3  o    6  o

You then build:

 /\
X  Y

Obtaining:

  /\
 /\ \
1 /\ \
 2 /\ \
  3  o/\
     4 /\
      5 /\
       6  o

which is ((1 2 3) 4 5 6 when represented with parenthesis. And this is a pair of lists.

tonio
Yes, I tried to simplify the drawing a bit, but that was not really a success. Will try to improve this.
tonio
Thank you for the diagram explanation, it makes sense now.
limist
A: 

Read this: http://en.wikipedia.org/wiki/Cons

Ori Pessach
A: 

try (list x y) I'm sure it works on common lisp, I don't know about Scheme

rabidmachine9
+7  A: 

'((1 2 3) 4 5 6) is actually a pair of lists. Here's another way to write it:

'((1 2 3) . (4 5 6))

However, the printer avoids dotted pair notation whenever it can, so you get the first representation instead. The rule is:

'(x . (xs ...))
=>
'(x xs ...)

For any x and xs. Here, your x = '(1 2 3) and xs = '(4 5 6), so you get ((1 2 3) 4 5 6).


To see how cons and dotted-pair notation is related, let's shorten the problem to just '(1) and '(6). The lowest level way to build a pair of them is this:

(cons (cons 1 '()) (cons 6 '()))

Here, '() is nil, or the empty list. If we translate this literally to dotted-pair notation, we get this:

'((1 . ()) . (6 . ()))

But because the printer collapses dotted-pair notation whenever possible, you get this instead:

'((1 . ()) . (6 . ()))
=>
'((1) . (6))    ; <-- x=1, xs=nothing; x=6, xs=nothing
=>
'((1) 6) ; <-- x=1, xs=6
Nathan Sanders
Thank you for the detailed explanation; I now see that it's a pair of lists, per the collapse of dotted pair notation, and since (cdr (cons x y)) gives the second list. But why does (length (cons x y)) answer 4? Shouldn't it be either 6 (count all elements of both lists), or 2 (count two lists)?
limist
Ah, never mind my comment; I see it's a consequence of the (length) function they give in the book that doesn't deal with nested lists.
limist
@limist: Well, sort of. Your problem (I think) is thinking of List as an opaque, separate data type. It's really just a convention of how to use cons pairs (see tonio's answer). So, to get length 6, you'd write `nested-list`, although this would really be called `tree-size` because it uses the tree convention for conses. On the other hand, to get 2 from `(length (cons '(1 2 3) '(4 5 6))`, you'd have to get 2 for *any* list. That method ignores the list convention that says that, eg, '(1) is actually `(cons 1 '())`. Its length is 1, not 2, because of the way lists are constructed.
Nathan Sanders
@Nathan: you're correct in your diagnosis, thank you. :) I realized there was vagueness in my mind about how cons and list are defined when I worked through another SICP problem, and also, some "legacy" mental model from how lists work in python. Thanks again for taking the time to explain things clearly, I appreciate it.
limist
+1  A: 

I found the diagrams in the Emacs Lisp tutorial particularly helpful when learning Lisp.

Ken