Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you get dot representation using 'list' command in LISP?

I understand that by using cons we could have something like :

> (cons 'b 'c)
(B . C)

And that happens because the cell is split into two and the values are B and C.

My question is, can you get the same result using list?

like image 694
Paul Avatar asked Mar 14 '26 23:03

Paul


1 Answers

You can't get list to return a dotted list, because "the last argument to list becomes the car of the last cons constructed" in the returned list.

But, you can get list* to return a dotted list. With the list* function, the final argument becomes the cdr of the last cons, so:

CL-USER> (list* 'a '(b))
(A B)
CL-USER> (list* 'a 'b '())
(A B)
CL-USER> (list* 'a 'b)
(A . B)
CL-USER> (list* 'a '(b c))
(A B C)
CL-USER> (list* 'a 'b '(c))
(A B C)
CL-USER> (list* 'a 'b 'c '())
(A B C)
CL-USER> (list* 'a 'b 'c)
(A B . C)

For example, (list* 'a 'b 'c '()) and (list 'a 'b 'c) are both equivalent to:

CL-USER> (cons 'a (cons 'b (cons 'c '())))
(A B C)

But (list* 'a 'b 'c) is equivalent to:

CL-USER> (cons 'a (cons 'b 'c))
(A B . C)

And, (list* 'a 'b) is equivalent to:

CL-USER> (cons 'a 'b)
(A . B)
like image 121
ad absurdum Avatar answered Mar 17 '26 03:03

ad absurdum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!