CONS,LIST,NOlist cons structures

来源:互联网 发布:冰川网络vr游戏上线没 编辑:程序博客网 时间:2024/05/22 00:52

1.CONS

The CONS function creates cons cells. It takes two inputs and

returns a pointer to a new cons cell whose CAR points to the first input and whose CDR points to the second. Then term "CONS" is shorted 

for CONStruct. If we try to explain CONS using parenthesis notation, we might say that CONS adds an element to the front of a list. For example, we can add the symbol A to the front of the list (B C D).


2.LIST

Creating a list from a bunch of elements is such a commom operation that Lisp has a built-in function to do just that. The LIST

function takes any numbers of inputs and makes a list of them.That is, it makes a new cons cell chain ending in NIL, with as many cells as there are inputs.


Notes: Let's look again at the difference between CONS and LIST. CONS makes a single cons cell. LIST makes a new cons cell chain list out of

however many inputs it receives.

3. Nonlist cons structures:

proper list is a cons cell chain ending in NIL. The convention is omit this NIL when writing lists in parenthesis notation, so the structure below is written (A B C).[Later,you will know it can written as (A B C . NIL), the same as (A B C),but usually we do not write that way.]



There are other sorts of cons cell structures that are not proper lists, because their chains

do not end in NIL. How can the structure below be represented in parenthesis notation ?


When printing a list in parenthesis notation, Lisp starts by printing  a left parenthesis

followed by all the elements, separated by space. Then, if the list ends in NIL,Lisp prints a 

right parenthesis. If it does not end in NIL, before printing the right parenthesis Lisp prints 

a space, a period, another space, and the atom that ends the chain. The list above, which

is called a dotted list rather than a proper list, is written like this:

(A B C . D)

So far, the only way we have to produce a cons cell structure that doesn't end in 

NIL is to use CONS.



The result of the CONS of A and B called a dotted pair. It is written (A . B) in parenthesis notation, while in cons cell notation it looks like this:

A dotted pair is a single cons cell whose CDR is not NIL.  The dotted list (A B . C) contains two cons cells, and is constructed this way:

In cons cell notation, (A B . C)looks like this:

Although LIST is often a more convenient tool than CONS for constructing lists,

the LIST function can only build proper list, since it always constructs a chain ending 

in NIL. For dotted lists CONS must be used.


原创粉丝点击