Home > Back-end >  How to create a list with a dotted-pair using only list function in lisp?
How to create a list with a dotted-pair using only list function in lisp?

Time:03-01

Let's say I have the following list: (A B . D) How I would reproduce this only by using list function ? (list 'A 'B '. 'D) is not working

CodePudding user response:

You cannot. The constructor to create lists are cons. Proper lists are just the empty list or a chain of one or more cons where the last one has the empty list as the cdr. list only creates proper lists. Eg. (list 'a 'b 'c) is a function that does (cons 'a (cons 'b (cons c '()))).

(a b . d) is created with (cons 'a (cons 'b 'd)). It is not a proper list since it is terminated by d and not nil. Thus it is impossible to create this structure with list.

CodePudding user response:

I'm not sure what you are really trying to do here, but this seems to be exactly what (list a b)

It is then easy to see that there is no value of b which will result in the cdr of the last cons in such a chain being other than nil (aka ()). Therefore lst, and hence list, cannot construct a chain of conses in which cdr of the last cons is other than nil, which means it cannot construct a dotted list, since that is the definition of a dotted list.

CodePudding user response:

We can almost do this with list, but not quite. The point is that list insists on putting a nil at the end of the list, yet we want to have a non-nil value at the end.

We can therefore have a dotted term anywhere but at the end of the list.

I can offer you:

(list 'a '(b . c) 'd) ; has a normal list end of 'd -> 'nil
(list 'a '(b . c))    ; looks like b . c is at the end, but actually 'a is
  • Related