-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariables.html
225 lines (222 loc) · 8.7 KB
/
variables.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
<html dir="ltr">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Arc: Variables</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">Previous: <a href="type.html">Type operations</a>
Up: <a href="index.html">Contents</a>
Next: <a href="networking.html">Networking</a>
</div>
<h1 class="links">Variables</h1>
<h2>Variables</h2>
<p><table class='arc'>
<tr>
<td class='arc'><a name='do'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>do</span> <span class='args'>[statement ...]</span>
<div class='desc'>Executes the statements in order. The do statement is useful when multiple statements need to be executed in a context that permits only a single statement, such as an 'if' clause.</div>
</td>
<td class='arc'><pre>
>(do (= x 2) (+ x 3))
<span class="return">5
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='do1'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>do1</span> <span class='args'>expr [body ...]</span>
<div class='desc'>Saves the first expression and returns it after executing the body. </div>
</td>
<td class='arc'><pre>
>(let x 42
(do1 x (= x 50)))
<span class="return">42
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='def'></a>
<img src='macro.gif' title='Macro'/>
<img src='proc.gif' title='Procedure'/>
<span class='op'>def</span> <span class='args'>name params [body ...]</span>
<div class='desc'>Defines a function with the given name, taking the specified parameters. This is the standard way of creating a new function in Arc. The parameter list params can be an empty list (for a function that takes no arguments), a variable (which will hold an arbitrary number of parameters as a list), or a dotted list (with the last variable holding any left-over parameters as a list).</div>
</td>
<td class='arc'><pre>
>(do (def foo x (prn x)) (foo 1 2 3))
<span class="stdout">(1 2 3)
</span><span class="return">(1 2 3)
</span></pre>
<pre>
>(do (def bar (a b c) (prn a "," b "," c)) (bar 1 2 3))
<span class="stdout">1,2,3
</span><span class="return">1
</span></pre>
<pre>
>(do (def baz (a . rest) (prn a "," rest)) (baz 1 2 3))
<span class="stdout">1,(2 3)
</span><span class="return">1
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defs'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defs</span> <span class='args'>[name params body] ...</span>
<div class='desc'>Performs multiple def operations. New in arc3.</div>
</td>
<td class='arc'><pre>
>(def foo x (prn x) bar (a b c) (prn a "." b "." c))
<span class="return">#<procedure: foo>
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defmemo'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defmemo</span> <span class='args'>name parms [body ...]</span>
<div class='desc'>Creates a memoized function.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='memo'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>memo</span> <span class='args'>f</span>
<div class='desc'>Creates a memoized function from f.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='safeset'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>safeset</span> <span class='args'>var value</span>
<div class='desc'>Sets var to value. This is similar to =, except it prints a warning to stderr if var is already defined.</div>
</td>
<td class='arc'><pre>
>(do (safeset var 1) (safeset var 2))
<span class="return">2
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='with'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>with</span> <span class='args'>([var val ...]) [body ...]</span>
<div class='desc'>Creates a new variable binding and executes the body. The values are computed before any of the assignments are done (like Scheme's <code>let</code>, rather than <code>let*</code>). If the last variable doesn't have a value, it is assigned nil.</div>
</td>
<td class='arc'><pre>
>(with (a 1 b 2 c 3) (+ a b c))
<span class="return">6
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='let'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>let</span> <span class='args'>var val [body ...]</span>
<div class='desc'>The let statement sets the variable var to the value within the scope of the body. Outside the let statement, any existing value of var is unaffected. Let is like <code>with</code> but with a single variable binding.</div>
</td>
<td class='arc'><pre>
>(let x 3 (+ x 1))
<span class="return">4
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='withs'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>withs</span> <span class='args'>([var val ...]) [body ...]</span>
<div class='desc'>Creates a new variable binding and executes the body. The values are computed sequentially (like Scheme's <code>let*</code>, rather than <code>let</code>). If the last variable doesn't have a value, it is assigned nil.</div>
</td>
<td class='arc'><pre>
>(withs (a 1 b (+ a 1)) (+ a b))
<span class="return">3
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='fn'></a>
<img src='foundation.gif' title='Foundation'/>
<span class='op'>fn</span> <span class='args'>args [body ...]</span>
<div class='desc'>fn is used to create lambda functions. The args can be a variable (which
will pick up all the arguments as a list), a list of variables, or a dotted list of
variables (the last will pick up the remainder as a list ).</div>
</td>
<td class='arc'><pre>
>((fn (x y) (+ x y)) 1 2)
<span class="return">3
</span></pre>
<pre>
>((fn all (len all)) 1 2 3)
<span class="return">3
</span></pre>
<pre>
>((fn (arg1 arg2 . rest) rest) 1 2 3 4)
<span class="return">(3 4)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='in'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>in</span> <span class='args'>x [choices ...]</span>
<div class='desc'>This predicate returns true if x is in the choices. Note that each choice is a separate argument.</div>
</td>
<td class='arc'><pre>
>(in 1 2 "b" 1 #\c)
<span class="return">t
</span></pre>
<pre>
>(in 1 '(1 2 3))
<span class="return">nil
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='copy'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>copy</span> <span class='args'>x [key val] ...</span>
<div class='desc'>Copies x, updating entries corresponding to the args.. The input must be of type sym, cons, string, or table. For a sym, additional arguments are not permitted. For a list, the keys are numeric indices into the list, and the corresponding entries are replaced with the values. For a string, the keys are indices into the string and the values must be characters. For a table, the keys and values add or update entries in the table. Original object x is unmodified.</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='sig'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>sig</span> <span class='args'> </span>
<div class='desc'>Hash table containing function signatures.</div>
</td>
<td class='arc'><pre>
>(sig 'map)
<span class="return">(f . seqs)
</span></pre>
</td></tr>
<tr>
<td class='arc'><a name='defhook'></a>
<img src='macro.gif' title='Macro'/>
<span class='op'>defhook</span> <span class='args'>name [body ...]</span>
<div class='desc'>Creates a function (similar to def), except the function is registered in hooks*</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='hook'></a>
<img src='proc.gif' title='Procedure'/>
<span class='op'>hook</span> <span class='args'>name [arg ...]</span>
<div class='desc'>Executes a function registered in hooks*</div>
</td>
<td class='arc'> </td></tr>
<tr>
<td class='arc'><a name='hooks*'></a>
<img src='var.gif' title='Variable'/>
<span class='op'>hooks*</span> <span class='args'></span>
<div class='desc'>(tests)</div>
</td>
<td class='arc'> </td></tr>
</table>
<p>
Copyright 2008 Ken Shirriff.
</body>
</html>