summaryrefslogtreecommitdiff
path: root/devdocs/elisp/circular-objects.html
blob: 797d97b1b27002d77814f3ce47e3b27312c95123 (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
 <h3 class="section">Read Syntax for Circular Objects</h3>     <p>To represent shared or circular structures within a complex of Lisp objects, you can use the reader constructs ‘<samp>#<var>n</var>=</samp>’ and ‘<samp>#<var>n</var>#</samp>’. </p> <p>Use <code>#<var>n</var>=</code> before an object to label it for later reference; subsequently, you can use <code>#<var>n</var>#</code> to refer the same object in another place. Here, <var>n</var> is some integer. For example, here is how to make a list in which the first element recurs as the third element: </p> <div class="example"> <pre class="example">(#1=(a) b #1#)
</pre>
</div> <p>This differs from ordinary syntax such as this </p> <div class="example"> <pre class="example">((a) b (a))
</pre>
</div> <p>which would result in a list whose first and third elements look alike but are not the same Lisp object. This shows the difference: </p> <div class="example"> <pre class="example">(prog1 nil
  (setq x '(#1=(a) b #1#)))
(eq (nth 0 x) (nth 2 x))
     ⇒ t
(setq x '((a) b (a)))
(eq (nth 0 x) (nth 2 x))
     ⇒ nil
</pre>
</div> <p>You can also use the same syntax to make a circular structure, which appears as an element within itself. Here is an example: </p> <div class="example"> <pre class="example">#1=(a #1#)
</pre>
</div> <p>This makes a list whose second element is the list itself. Here’s how you can see that it really works: </p> <div class="example"> <pre class="example">(prog1 nil
  (setq x '#1=(a #1#)))
(eq x (cadr x))
     ⇒ t
</pre>
</div> <p>The Lisp printer can produce this syntax to record circular and shared structure in a Lisp object, if you bind the variable <code>print-circle</code> to a non-<code>nil</code> value. See <a href="output-variables">Output Variables</a>. </p><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/Circular-Objects.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Circular-Objects.html</a>
  </p>
</div>