User Tools

Site Tools


ch1_4_conscells

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ch1_4_conscells [2024/05/04 09:45] adminch1_4_conscells [2024/05/05 11:23] (current) admin
Line 3: Line 3:
 //There is no list// //There is no list//
  
-We have learned that Lisp expressions consist of atoms and lists, but in fact, there is no //list// object in the background. A list in Lisp is constructed with objects called //conscells//, or simply //cons//.+We have learned that Lisp expressions consist of atoms and lists, but in fact, there is no //list// object in the background. A list in Lisp is constructed with objects called //conscells//, or simply ''CONS''.
  
 Conscell is an object holding two pointers, the first points to an element of the list and the second points to the next conscell. That means Lisp lists are //unidirectional linked lists//. Conscell is an object holding two pointers, the first points to an element of the list and the second points to the next conscell. That means Lisp lists are //unidirectional linked lists//.
  
-For historical reasons, the pointers are called ||car|| and ||cdr||.+For historical reasons, the pointers are called //car// and //cdr// . 
 + 
 +{{ :wiki:cons.png?100 |}} 
 + 
 +For example, list with a sublist ''(a (b c) d)'', will be stored like this: 
 + 
 +{{ :wiki:abcd_list_cons.png?200 |}} 
 + 
 +Proper list ends with a conscell, which has ''NIL'' (null pointer) in //cdr//. 
 + 
 +Lisp allows us to construct list directly using ''CONS'' function. 
 + 
 +<code lisp> 
 +>(cons 'a (cons 'b (cons 'c nil))) 
 +(A B C) 
 +</code>  
 + 
 +The code above produces exactly the same list as when using ''LIST'' function, or simply quoted list expression: 
 + 
 +<code lisp> 
 +>(list 'a 'b 'c) 
 +(A B C) 
 + 
 +>'(a b c) 
 +(A B C) 
 +</code>  
 + 
 +The power and simplicity of Lisp originates from the fact that code is a list of lists and atoms, exactly the same structure connected with conscells, as is any user-defined data list. Lisp macros can manipulate the code using any of the functions that manipulate data lists. 
 + 
 +FIXME 
 + 
 +Show more examples, cons, car, cdr, link to List Processing Functions. 
 +Show functionality of accessors and SETF shenanigans. Link to Cycles. 
 +Dotting. 
 +Show list with nil element, or (nil) sublist. 
 + 
ch1_4_conscells.1714837502.txt.gz · Last modified: 2024/05/04 09:45 by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki