[Beginner] cdr and list of lists trouble

Discussion of Common Lisp

[Beginner] cdr and list of lists trouble

Postby nikond3s » Mon Oct 03, 2011 10:43 am

Hi,

this might be a silly question but I miss something and do not know what it is. I'm using the CLISP interpreter for my "experiments".

Given the list of lists ((a b c) (d e f)) and after applying car/cdr the output looks like the following.

> (car '((a b c) (d e f)))
> (A B C)
> (cdr '((a b c) (d e f)))
> ((D E F))

I'm helpless about the cdr output. Why is it a "list of lists" and not just a single list like the result after applying car?

regards

PS: I hope my english is somehow understandable, it is not my native language. :)
nikond3s
 
Posts: 9
Joined: Mon Oct 03, 2011 10:05 am

Re: [Beginner] cdr and list of lists trouble

Postby Ramarren » Mon Oct 03, 2011 10:56 am

nikond3s wrote:Why is it a "list of lists" and not just a single list like the result after applying car?


Why would you expect it to be? CDR when applied to a list gives you a list without the first element of the list. In fact, in Common Lisp it is synonymous with the function REST.

Read something like A Gentle Introduction to Symbolic Computation for more comprehensive explanation of how lists are constructed in Common Lisp.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: [Beginner] cdr and list of lists trouble

Postby nikond3s » Mon Oct 03, 2011 11:31 am

Ramarren wrote:
nikond3s wrote:Why is it a "list of lists" and not just a single list like the result after applying car?

Why would you expect it to be? CDR when applied to a list gives you a list without the first element of the list.


Why I would expect it to be, is because if I apply "tail" onto a list I'd expect the rest as list and not a list containing the rest, which maybe not accurate. I realized that my understanding of CDR was correct but my understanding of the "cons"-structure wasn't. CDR applied onto a list just points to the next cons-cell on which I need to apply CAR to point to (in my case) the list it contains. I saw it after reading page 47-49 in the book you provided.

Thank you for the book, its very easy to understand. Regards.
nikond3s
 
Posts: 9
Joined: Mon Oct 03, 2011 10:05 am

Re: [Beginner] cdr and list of lists trouble

Postby Ramarren » Mon Oct 03, 2011 12:09 pm

nikond3s wrote:Why I would expect it to be, is because if I apply "tail" onto a list I'd expect the rest as list and not a list containing the rest, which maybe not accurate.


Even disregarding the details of conses, you have a list of lists, which means that the inner lists are just elements. Consider, if you had a list of numbers (1 2 3 4 5) then the tail of the list would be (2 3 4 5) which is necessarily a list. In your case there is a single element, which happens to be itself a list, but that doesn't change how CDR works.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: [Beginner] cdr and list of lists trouble

Postby nikond3s » Mon Oct 03, 2011 12:53 pm

I think I got it now. I saw "too much" which wasn't really there. In my example CAR returns the list it is pointing to, which is the first element of the "parent"-list. CDR returns a list containing the rest where the first element happens to be a list. Thats it nothing special at all. I don't know what took me so long...;).
nikond3s
 
Posts: 9
Joined: Mon Oct 03, 2011 10:05 am


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 2 guests