Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>R: "Look, in the sky! The Lambda Signal! A citizen is in trouble Lambda Man!"</em></p> <p><em>LM: "I see it! And I've got just the box art they need."</em></p> <hr> <p>In Lisps, lists are singly-linked data structures, comprised of elements called <em>cons cells</em>. Each of these cells is a structure that consists of</p> <ol> <li>a pointer to a value </li> <li>a pointer to the next cell </li> </ol> <p>These are called the <em>car</em> and <em>cdr</em> respectively for historical reasons. Here's the traditional box art representing a 3-element list:</p> <pre><code>Structure: (car . cdr -)---&gt;(car . cdr -)---&gt;(car . cdr) | | | | v v v v Values: 1 2 3 nil </code></pre> <p>The <code>car</code> and <code>cdr</code> functions allow you to work with lists from this low level of abstraction, and return the values of the respective cells. Thus, <code>car</code> returns the 'value' of the cell, and <code>cdr</code> dereferences to the remainder of the list. <code>nthcdr</code> is a generalisation on top of <code>cdr</code> for convenience.</p> <p>The value returned by <code>cdr</code> is a reference to a raw data structure, which is mutable at this level. If you change the value of a cons-cell's cdr, you are changing the underlying structure of the list.</p> <p>Given:</p> <pre><code> let A = '(1 2) ~= (1 . -)--&gt;(2 . nil) let B = '(3 4) ~= (3 . -)--&gt;(4 . nil) </code></pre> <p>Setting the cdr of <code>(cdr A)</code> to B will destructively concatenate A and B such that A is now the structure below:</p> <pre><code> A B (1 . -)--&gt;(2 . -)--&gt;(3 . -)--&gt;(4 . nil) </code></pre> <p>As we've shown, a <code>nil</code> value in a cell's cdr represents the end of the list - there's nothing more that can be traversed. If we set the cdr of A to <code>nil</code>, we lobotomise the list, such that A is now</p> <pre><code>A (1 . nil) &lt;- [Not pointing to anything - the rest of the list shall go wanting] </code></pre> <p>This is pretty much what you've done - you've mutated the underlying data structures using low-level functions. :) By setting one of the cell's cdrs to <code>nil</code>, you've trimmed the end off your list.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload