Hi!two very quick noobish questions

Discussion of Common Lisp

Hi!two very quick noobish questions

Hi guys!
i'm a total noob at lisp,so please excuse me if you find those questions dumb:
i conclude from the code below that '(1 . 2) just makes one single cons with its car pointing to 1 and its cdr pointing to 2,
while '(1 2) will create a list of two cons,with their car respectively pointing to 1 and 2.
Code: Select all
`>(car (cdr '(1 2))) 2>(car (cdr '(1 . 2)))error:2 is a value,etc...`

so my first question is:
what is the real difference beetween '(1 2) and '(1 . 2),and if my conclusion is correct,why would anyone want to use
something like '(1 . 2)...is it something like a tuple?

and now about the second question:
well it's about what (cons 4 lst) does.i know that it returns a new list,but is the list returned a deep or shallow copy of the first one with an additional cons
at its begining?in other words,does (cons 4 lst) just make a new cons with a car pointing to 4 an a cdr pointing to (car lst),or does it make a new list from
scratch?
The_Ash

Posts: 6
Joined: Sun Mar 04, 2012 10:21 am

Re: Hi!two very quick noobish questions

You might want to read a book like Gentle Introduction to Symbolic Computation for a more detailed explanation.

The_Ash wrote:what is the real difference beetween '(1 2) and '(1 . 2),and if my conclusion is correct,why would anyone want to use
something like '(1 . 2)...is it something like a tuple?

The difference is as you said: the former is two conses and the latter is a single one. Every cons cell requires a little bookkeeping, and the pointer to NIL object which terminates a proper list also consumes memory. In the past this could have been possibly relevant, for modern systems the difference doesn't really matter. The only real common use for dot syntax is for destructuring, where it can signify the tail of the list.

The_Ash wrote:well it's about what (cons 4 lst) does.

CONS constructs a new cons cell, which points to the arguments of the function. That is the only thing it does, and in particular it doesn't copy anything.
Ramarren

Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Hi!two very quick noobish questions

Thx a lot!i'll start reading those right now!
The_Ash

Posts: 6
Joined: Sun Mar 04, 2012 10:21 am

Re: Hi!two very quick noobish questions

Beside what was already said: usually there would be functions of a pattern c[ad]+r that perform what you did by sequentially calling car and cdr.

In other words: cadr is the same as calling car, then cdr, cddr is the same as calling cdr then cdr, caar is the same as calling car, then car and so on. I think it depends on the particular variant of Lisp you are using, but there would be usually at least 4 levels. See here for more info: http://www.lispworks.com/documentation/ ... _car_c.htm
wvxvw

Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Who is online

Users browsing this forum: No registered users and 8 guests