File: circular-lists.htm

package info (click to toggle)
nyquist 3.12%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 58,036 kB
  • sloc: ansic: 74,355; lisp: 20,485; java: 9,390; cpp: 6,695; sh: 207; xml: 58; makefile: 40
file content (133 lines) | stat: -rw-r--r-- 3,314 bytes parent folder | download | duplicates (7)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<html><head>

<title>Circular Lists</title>

<style type="text/css">
.example {
  color: #000000;
  background-color: #F5F5F5;
  padding: 8px;
  border: #808080;
  border-style: solid;
  border-width: 1px;
  width:auto;
}
.button {
  color: #000000;
  background-color: #F5F5F5;
  padding-top: 1px;
  padding-bottom: 1px;
  padding-left: 4px;
  padding-right: 8px;
  border: #808080;
  border-style: solid;
  border-width: 1px;
  white-space: pre;
}
.box {
  color: #000000;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 16px;
  padding-right: 16px;
  border: #808080;
  border-style: solid;
  border-width: 1px;
}
</style>

</head>

<body>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="examples.htm">Examples</a> |
<a href="../reference/reference-index.htm">Reference</a>

<hr>

<h1>Circular Lists</h1>

<hr>


<hr>

<h2>Known XLISP Problems with Circular Lists</h2>

<hr>

<p><b>Warning:</b> do <b>not</b> try this with XLISP:</p>

<pre class="example">
&gt; (setq my-list (cons 'item nil))  <font color="#008844">; create a 1-item list</font>
(ITEM)

&gt; (setf (cdr my-list) my-list))    <font color="#008844">; create the circle</font>
ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM
ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM
ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM
ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ITEM ...
</pre>

<p>If you're lucky you can <a href="../reference/break.htm">break</a> the
loop. <nobr>If not</nobr>, then XLISP will print the ITEM forever.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="circular-nth"></a>

<hr>

<h2>c-nth</h2>

<hr>

<p>The '<nobr>c-nth</nobr>' macro accesses a linear list as if it were
circular:</p>

<pre class="example">
(defmacro <font color="#0000CC">c-nth</font> (index list)
  `(nth ,(rem index (length list)) ,list))
</pre>

<p>Note that with every call to '<nobr>c-nth</nobr>', the length of the list
will be computed again.</p>

<p>Examples:</p>

<pre class="example">
(c-nth 0 '(1 2 3))  =&gt; 1
(c-nth 1 '(1 2 3))  =&gt; 2
(c-nth 2 '(1 2 3))  =&gt; 3
(c-nth 3 '(1 2 3))  =&gt; 1
(c-nth 4 '(1 2 3))  =&gt; 2
(c-nth 5 '(1 2 3))  =&gt; 3
(c-nth 6 '(1 2 3))  =&gt; 1
(c-nth 7 '(1 2 3))  =&gt; 2
</pre>

Because '<nobr>c-nth</nobr>' is a macro expanding into a regular
<a href="../reference/nth.htm">nth</a> form, '<nobr>c-nth</nobr>' can be
used <nobr>with <a href="../reference/setf.htm">setf</a></nobr>:

<pre class="example">
(setq lst '(1 2 3))      =&gt; (1 2 3)
lst                      =&gt; (1 2 3)
(setf (c-nth 4 lst) 'x)  =&gt; X
lst                      =&gt; (1 X 3)
</pre>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<hr>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<a href="../manual/contents.htm">Contents</a> |
<a href="../tutorials/tutorials.htm">Tutorials</a> |
<a href="examples.htm">Examples</a> |
<a href="../reference/reference-index.htm">Reference</a>

</body></html>