summaryrefslogtreecommitdiff
path: root/devdocs/elisp/box-diagrams.html
blob: f30af8eb0722071de7e7d48837124baff5959f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 <h4 class="subsubsection">Drawing Lists as Box Diagrams</h4>   <p>A list can be illustrated by a diagram in which the cons cells are shown as pairs of boxes, like dominoes. (The Lisp reader cannot read such an illustration; unlike the textual notation, which can be understood by both humans and computers, the box illustrations can be understood only by humans.) This picture represents the three-element list <code>(rose violet buttercup)</code>: </p> <div class="example"> <pre class="example">    --- ---      --- ---      --- ---
   |   |   |--&gt; |   |   |--&gt; |   |   |--&gt; nil
    --- ---      --- ---      --- ---
     |            |            |
     |            |            |
      --&gt; rose     --&gt; violet   --&gt; buttercup
</pre>
</div> <p>In this diagram, each box represents a slot that can hold or refer to any Lisp object. Each pair of boxes represents a cons cell. Each arrow represents a reference to a Lisp object, either an atom or another cons cell. </p> <p>In this example, the first box, which holds the <small>CAR</small> of the first cons cell, refers to or holds <code>rose</code> (a symbol). The second box, holding the <small>CDR</small> of the first cons cell, refers to the next pair of boxes, the second cons cell. The <small>CAR</small> of the second cons cell is <code>violet</code>, and its <small>CDR</small> is the third cons cell. The <small>CDR</small> of the third (and last) cons cell is <code>nil</code>. </p> <p>Here is another diagram of the same list, <code>(rose violet
buttercup)</code>, sketched in a different manner: </p> <div class="example"> <pre class="example"> ---------------       ----------------       -------------------
| car   | cdr   |     | car    | cdr   |     | car       | cdr   |
| rose  |   o--------&gt;| violet |   o--------&gt;| buttercup |  nil  |
|       |       |     |        |       |     |           |       |
 ---------------       ----------------       -------------------
</pre>
</div>   <p>A list with no elements in it is the <em>empty list</em>; it is identical to the symbol <code>nil</code>. In other words, <code>nil</code> is both a symbol and a list. </p> <p>Here is the list <code>(A ())</code>, or equivalently <code>(A nil)</code>, depicted with boxes and arrows: </p> <div class="example"> <pre class="example">    --- ---      --- ---
   |   |   |--&gt; |   |   |--&gt; nil
    --- ---      --- ---
     |            |
     |            |
      --&gt; A        --&gt; nil
</pre>
</div> <p>Here is a more complex illustration, showing the three-element list, <code>((pine needles) oak maple)</code>, the first element of which is a two-element list: </p> <div class="example"> <pre class="example">    --- ---      --- ---      --- ---
   |   |   |--&gt; |   |   |--&gt; |   |   |--&gt; nil
    --- ---      --- ---      --- ---
     |            |            |
     |            |            |
     |             --&gt; oak      --&gt; maple
     |
     |     --- ---      --- ---
      --&gt; |   |   |--&gt; |   |   |--&gt; nil
           --- ---      --- ---
            |            |
            |            |
             --&gt; pine     --&gt; needles
</pre>
</div> <p>The same list represented in the second box notation looks like this: </p> <div class="example"> <pre class="example"> --------------       --------------       --------------
| car   | cdr  |     | car   | cdr  |     | car   | cdr  |
|   o   |   o-------&gt;| oak   |   o-------&gt;| maple |  nil |
|   |   |      |     |       |      |     |       |      |
 -- | ---------       --------------       --------------
    |
    |
    |        --------------       ----------------
    |       | car   | cdr  |     | car     | cdr  |
     ------&gt;| pine  |   o-------&gt;| needles |  nil |
            |       |      |     |         |      |
             --------------       ----------------
</pre>
</div><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Box-Diagrams.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Box-Diagrams.html</a>
  </p>
</div>