-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment.html
390 lines (385 loc) · 14.4 KB
/
assignment.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Assignment</title>
<link rel="shortcut icon" href="/assets/favicon.png">
<link rel="stylesheet" type="text/css" href="code.css">
<link href="/assets/bootstrap.css" rel="stylesheet">
</head>
<body style='margin:12px 50px 0'>
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<ul class="nav navbar-nav">
<li><a href="/ref/">Arc 3.1</a>
<li><a href="http://tryarc.org">Try it</a></li>
<li><a href="http://github.com/arclanguage/anarki">Get it</a></li>
<li><a href="http://ycombinator.com/arc/tut.txt">Tutorial</a></li>
<li><a href="http://arclanguage.org/forum">Forum</a></li>
</ul>
</div>
</div> <!-- end of navbar -->
<div class="links">Up: <a href="index.html">Contents</a>
Next: <a href="combining.html">Boolean operations</a>
</div>
<h1 class="links">Assignment</h1>
The Arc language features a number of assignment functions to update variables and data structures. Arc also includes the concept of a "place", which specifies the position to be updated. Places are also known as "generalized variables", as they can be used as variables in many contexts.
<p>
This page describes Arc's basic assignment operators as well as the operators that use places to perform updates.
<h2>Assignment primitives</h2>
The simplest assignment operator in Arc is
<code>(set <i>symbol</i> <i>value</i>)</code>.
The <code>set</code> form can also be used to set multiple variables in once statement:
<code>(set <i>symbol1</i> <i>value1</i> <i>symbol2</i> <i>value2 ...</i>)</code>.
<p>A list can be destructively modified with <code>scar</code> or <code>scdr</code>, which update the first element of a list or the remainder of a list, respectively. Note that these operations do not create a new copy of the list; they modify the existing list. The modifications will affect any other symbols referring to the list.
<pre class="repl">
arc> (set m '(1 2 3) n m)
(1 2 3)
arc> (scar m 'a)
a
arc> n
(a 2 3)
arc> (scdr n '(b c))
(b c)
arc> m
(a b c)
</pre>
Unusual list structures, including cyclic lists, can be created by splicing lists together with <code>scar</code> or <code>scdr</code>. Arc currently hangs if a cyclic list is displayed, so be forewarned.
<h2>Places and =</h2>
Generallly, assignment in Arc is performed with the <code>=</code> macro.
It is similar to <code>set</code>, except it allows assignment not only to a symbol, but also to a "place".
Roughly speaking, a place is a location that can be updated.
The place can be a symbol, an index into a table, a character in a string, an index into a list, or a complex location in a list. The following examples illustrate how these different places can be updated.
<pre class="repl">
arc> (= mytable (table) mystring "abcde" mylist '((a b) c (d 3)))
((a b) c (d 3))
arc> (= (mytable "key") 42)
42
arc> mytable
#hash(("key" . 42))
arc> (= (mystring 2) #\x)
#\x
arc> mystring
"abxde"
arc> (= (mylist 1) 'z)
z
arc> (= (car (car mylist)) 'y)
y
arc> mylist
((y b) z (d 3))
</pre>
Because of its convenience, <code>=</code> is the operator used most often for assignment and updates in Arc. It can also be extended to support new types of places; this is considerably more complex, and will be discussed in a later article.
<h2>Operations on places</h2>
Several other macros allow assignment to places. <code>wipe</code> and <code>assert</code> allow places to be set to <code>nil</code> or <code>t</code> respectively. The name of <code>assert</code> may cause confusion; it is used to set a place, not to assert that something is true.
<pre class="repl">
arc> (= a 1 b 2 c 3)
3
arc> (wipe a b c)
nil
arc> a
nil
arc> (assert a b c)
t
arc> b
t
</pre>
<p>
<code>(swap <i>place1 place2</i>)</code> will swap the values at the two places. The places do not necessarily need to be in the same data structure and do not necessarily need to be lists.
<pre class="repl">
(= x '(a b c d e))
arc> (swap (car x) (x 3))
arc> x
(d b c a e)
arc> (= s "abc")
arc> (swap (s 0) (s 2))
arc> s
"cba"
</pre>
<code>(rotate <i>place1 place2 ...</i>)</code> will rotate the places to the left. That is, the value from <code><i>place2</i></code> will go in <code><i>place1</i></code>, the value from
<code><i>place3</i></code> will go in
<code><i>place2</i></code>, and so on, and the value from <code><i>place1</i></code> will go in the last place.
<pre class="repl">
arc> (= x '(a b c d e))
arc> (rotate (x 0) (x 2) (x 4))
arc> x
(c b e d a)
</pre>
<p>Several macros modify the value at a place.
Similar to the C operators, <code>++</code> and <code>--</code> will decrement or increment a place. The place is modified by 1 by default, but the amount of increment or decrement can be specified.
<pre class="repl">
arc> (= x '(0 0))
(0 0)
arc> (++ (x 0))
1
arc> (-- (x 1) 10)
-10
arc> x
(1 -10)
</pre>
More general read-modify-write modifications can be done with <code>(zap <i>op place [args]</i>)</code>, which gets the value at a place, applies the operation to the value and optional arguments, and puts the value back at the place.
<pre class="repl">
arc> (let s "abc" (zap upcase (s 0)) s)
"Abc"
arc> (let x '(10 10) (zap mod (car x) 3) x)
(1 10)
arc> (let tb (table) (= (tb "key") "val") (zap + (tb "key") "stuff") tb)
#hash(("key" . "valstuff"))
</pre>
<h2>Places and lists</h2>
Lists can be modified with
<code>push</code>, <code>pushnew</code>, <code>pop</code>, and <code>pull</code>.
<code>(push <i>obj place</i>)</code> inserts the object before the place.
<code>pushnew</code> is similar to <code>push</code>, except the object is pushed only if it is new: if it is not already in the list.
<pre class="repl">
arc> (let x '(a b c d e) (push 'z x) (push 'b x) (push 'w x) x)
(w b z a b c d e)
arc> (let x '(a b c d e) (pushnew 'z x) (pushnew 'b x) (pushnew 'w x) x)
(w z a b c d e)
</pre>
<code>(pop <i>place</i>)</code> returns the object at the place, and removes that object from the list.
<pre class="repl">
arc> (= x '(a b c d e))
(a b c d e)
arc> (pop x)
a
arc> x
(b c d e)
</pre>
Elements can be filtered from a list with <code>(pull <i>test place</i>)</code>. Objects satisfying <code><i>test</i></code> are removed from the list, and the updated list is returned. Note that the values kept and returned are the values that fail the test. For example, to pull the odd elements out of a list:
<pre class="repl">
arc> (= x '(1 2 3 5 8 13))
(1 2 3 5 8 13)
arc> (pull odd x)
(2 8)
</pre>
<p>Typically the place for these operations indicates a list, but the place doesn't necessarily need to be the beginning of the list. For instance, <code>cdr</code> can be used to access a place inside the list.
<pre class="repl">
arc> (let x '(a b c d e) (push 'z (cdr (cdr x))) x)
(a b z c d e)
arc> (let x '(a b c d e) (pop (cdr x)))
b
</pre>
<h2>Summary</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='assign'></a>
<img src='foundation.gif' title='Foundation'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>assign</span> <span class='args'>symbol expr</span>
<div class='desc'>assign is used to assign a value to a variable. Renamed from <code>set</code> in arc3.</div>
</td>
<td class='arc'><pre>
>(assign x 10)
<span class="return">10
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='scar'></a>
<img src='foundation.gif' title='Foundation'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>scar</span> <span class='args'>list expr</span>
<div class='desc'>Assigns an expression to the car of list. If applied to a
string, assigns to the first character of the string, which must have length at
least one.</div>
</td>
<td class='arc'><pre>
>(do
(= x (copy "abc"))
(scar x #\d)
x)
<span class="return">"dbc"
</span></pre>
<pre>
>(do
(= x '(1 2 3))
(scar x #\d)
x)
<span class="return">(#\d 2 3)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='scdr'></a>
<img src='foundation.gif' title='Foundation'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>scdr</span> <span class='args'>list exp</span>
<div class='desc'>Assigns to cdr of a list.</div>
</td>
<td class='arc'><pre>
>(do
(= x '(1 2 3))
(scdr x '(4))
x)
<span class="return">(1 4)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='%3d'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>=</span> <span class='args'>[place expr] ... [place]</span>
<div class='desc'>Assigns to each place to the associated expression. If the last place has no associated expression, it is set to nil.</div>
</td>
<td class='arc'><pre>
>(= x 1)
<span class="return">1
</span></pre>
<pre>
>(= x 2 y 4)
<span class="return">4
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='wipe'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>wipe</span> <span class='args'>[place ...]</span>
<div class='desc'>Assigns nil to the places. Typically, the places are simple variables.</div>
</td>
<td class='arc'><pre>
>(do (wipe a b c) (list a b c))
<span class="return">(nil nil nil)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='set'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>set</span> <span class='args'>[place ...]</span>
<div class='desc'>Assigns t to the places. Renamed from assert in arc3.</div>
</td>
<td class='arc'><pre>
>(do (set a b c) (list a b c))
<span class="return">(t t t)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='swap'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>swap</span> <span class='args'>place1 place2</span>
<div class='desc'>The contents of the two places are swapped. The new contents of <code>place2</code> are returned.</div>
</td>
<td class='arc'><pre>
>(with (x 'a y '(1 2))
(swap x y)
(prn "x:" x)
y)
<span class="stdout">x:(1 2)
</span><span class="return">a
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='rotate'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>rotate</span> <span class='args'>[place1 place2 ...]</span>
<div class='desc'>Assigns <i>place2</i> to <i>place1</i>, assigns <i>place3</i> to </i>place2</i>, and so on, assigning <i>place1</i> to the last place.</div>
</td>
<td class='arc'><pre>
>(let s (copy "abc") (rotate (s 0) (s 1) (s 2)) s)
<span class="return">"bca"
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='%2b%2b'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>++</span> <span class='args'>place [i]</span>
<div class='desc'>Increments the value at <code>place</code> by <code>i</code>. The default increment is 1.</div>
</td>
<td class='arc'><pre>
>(let x '(10 20) (++ (car x)) (++ (x 1) 5) x)
<span class="return">(11 25)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='--'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>--</span> <span class='args'>place [i]</span>
<div class='desc'>Decrements the value at <code>place</code> by <code>i</code>. The default decrement is 1.</div>
</td>
<td class='arc'><pre>
>(let x '(10 20) (-- (car x)) (-- (x 1) 5) x)
<span class="return">(9 15)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='zap'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>zap</span> <span class='args'>op place [args ...]</span>
<div class='desc'>Gets the value at the place, evaluates <code>(op <i>value args...</i>)</code>, and stores the result in the place.</div>
</td>
<td class='arc'><pre>
>(let x '(0 10 20) (zap * (x 1) 5) x)
<span class="return">(0 50 20)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='push'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>push</span> <span class='args'>elt place</span>
<div class='desc'>Pushes an element at the beginning of the list referenced by place. The list is modified and returned.</div>
</td>
<td class='arc'><pre>
>(let x '(1 2 3) (push 'a x) x)
<span class="return">(a 1 2 3)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='pushnew'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>pushnew</span> <span class='args'>elt place [test]</span>
<div class='desc'>Pushes <code>elt</code> before the place if it is not present in the list. The equality test can be specified; <code>iso</code> is used by default. </div>
</td>
<td class='arc'><pre>
>(let x '(1 2 3) (pushnew 'a x) x)
<span class="return">(a 1 2 3)
</span></pre>
<pre>
>(let x '(1 2 3) (pushnew 2 x) x)
<span class="return">(1 2 3)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='pop'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>pop</span> <span class='args'>place</span>
<div class='desc'>The first element is removed from place and returned. If the value at the place is nil, then nil is returned.</div>
</td>
<td class='arc'><pre>
>(let x '(1 2 3)
(prn "Popped:" (pop x))
x)
<span class="stdout">Popped:1
</span><span class="return">(2 3)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='pull'></a>
<img src='macro.gif' title='Macro'/>
<img src='destructive.gif' title='Destructive'/>
<span class='op'>pull</span> <span class='args'>test place</span>
<div class='desc'>Remove elements satisfying <code>test</code> from the list starting at <code>place</code>.</div>
</td>
<td class='arc'><pre>
>(let x '(1 100 2 50 3) (pull [< _ 10] x) x)
<span class="return">(100 50)
</span></pre>
</td></tr>
</table>
<h3>Legend</h3>
<img src='/doc/foundation.gif' title='Foundation'/>: Foundation operation.
<br><img src='/doc/macro.gif' title='Macro'/>: Macro.
<br><img src='/doc/proc.gif' title='Procedure'/>: Procedure.
<br><img src='/doc/var.gif' title='Variable'/>: Variable.
<br><img src='/doc/destructive.gif' title='Destructive'/>: Destructive; arguments may be modified.
<br><img src='/doc/predicate.gif' title='Predicate'/>: Predicate.
<br><img src='/doc/code.gif' title='View code'/>: View code.
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>