-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassign.html
139 lines (136 loc) · 5.79 KB
/
assign.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
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>