hank / life

Good code.

This URL has Read+Write access

life / oscon / 2008 / tutorials / Practical.Erlang.html
100644 352 lines (309 sloc) 6.043 kb
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>RDoc Documentation</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h2>File: Practical.Erlang.rdoc</h2>
<table>
  <tr><td>Path:</td><td>Practical.Erlang.rdoc</td></tr>
  <tr><td>Modified:</td><td>Tue Jul 22 16:11:39 -0700 2008</td></tr>
</table>
 
<h1>OSCON 2008, Tutorial 4: Practical Erlang</h1>
<p>
This tutorial is not about the history of Erlang, comparing Erlang to other
languages, or a comprehensive overview.
</p>
<p>
The goal is that we can read and write erlang, understand the concurrency
model and the error handling model.
</p>
<h2>Basic Erlang</h2>
<h3>Starting the system</h3>
<pre>
  erl
</pre>
<p>
You get some *1&gt;* and *2&gt;* stuff. End everything with a .
</p>
<h3>Data Types</h3>
<h4>Integers</h4>
<ul>
<li>10
 
</li>
<li>-234
 
</li>
<li>16#16 = conversion from hex
 
</li>
<li>2#101010 = conversion from binary
 
</li>
<li>$A = ASCII value of A
 
</li>
</ul>
<p>
There&#8216;s also floats.
</p>
<h4>Atoms</h4>
<p>
Constant literals, always lowercase letter or in quotes.
</p>
<h4>Tuples</h4>
<p>
Fixed number of elements, any size, and contain any valid Erlang
expression.
</p>
<h4>Lists</h4>
<ul>
<li>Variable number of elements
 
</li>
<li>dynamic size
 
</li>
<li>strings are lists of characters
 
</li>
<li>lists are encapsulated by *[]*
 
</li>
<li>empty string is an empty list
 
</li>
</ul>
<h5>Concatenation</h5>
<pre>
  [1,2,3] ++ [4,5,6]
</pre>
<h4>Complex Data Structures</h4>
<ul>
<li>Just a big list generally, containing tuples.
 
</li>
<li>Uses a garbage collector.
 
</li>
</ul>
<h3>Pattern Matching</h3>
<ul>
<li>Pattern = Expression
 
</li>
</ul>
<h4>Assignment</h4>
<pre>
  {B,C,D} = {10,foo,bar}.
  [H|T] = [1,2,3]
</pre>
<h4>Testing</h4>
<pre>
  {A,A,B} = {abc,def,123}.
</pre>
<p>
Fails because A is being reassigned
</p>
<pre>
  [A,B,C,D] = [1,2,3]
</pre>
<p>
Fails because the lists are not the same size
</p>
<pre>
  [A,B|C] = [1,2,3,4,5,6,7]
</pre>
<p>
Suceeds, A=1, B=2, and C=the rest
</p>
<pre>
  [H|T] = []
</pre>
<p>
Fails since at least 1 element is needed on the right
</p>
<h3>Variables</h3>
<pre>
  {A,_, [B|_], {B}} = {abc,23,[22,x], {22}}
</pre>
<p>
Succeeds - A=abc, B=22
</p>
<h3>Functions</h3>
<ul>
<li>You have to define functions with the correct arity.
 
</li>
<li>Need to be exported to be used outside
 
</li>
</ul>
<h2>Sequential Erlang</h2>
<h3>Conditionals</h3>
<h4>Case</h4>
<pre>
  case lists:member(foo,List) of
    true -&gt; ok;
    false -&gt; {error, unknown}
  end
</pre>
<ul>
<li>No punctuation on last <b>case</b> clause.
 
</li>
<li>Can evaluate one or more expressions per clause, seperated by ;.
 
</li>
<li>There&#8216;s a dont-care clause that can protect you from a runtime error.
 
</li>
</ul>
<h4>If</h4>
<pre>
  if
    X&lt;1 -&gt; smaller;
    X&gt;1 -&gt; greater;
    X==1 -&gt; equal
  end
</pre>
<ul>
<li>true guard allows a default case. It&#8216;s not mandatory.
 
</li>
</ul>
<h4>Guards</h4>
<p>
We can use guards in functions using <b>when</b>.
</p>
<pre>
  factorial(N) when N &gt; 0 -&gt;
    N * factorial(N-1);
  factorial(0) -&gt; 1.
</pre>
<p>
Since everything is pattern matched sequentially, we get a performance
improvement by having the N&gt;0 case first.
</p>
<pre>
  is_number
  is_atom
  length
  X == Y % Simply compares values
  X =:= Y % Also compares data types
</pre>
<p>
All guards have to succeed here:
</p>
<pre>
  swap(String) when is_list(String),
                    length(String) &lt; 10 -&gt; ...
</pre>
<p>
Only one guard has to succeed here:
</p>
<pre>
  swap(Tuple) when is_tuple(Tuple);
                   is_atom(Tuple) -&gt; ...
</pre>
<h3>Recursion</h3>
<h4>Traversing lists</h4>
<pre>
  average(X) -&gt; sum(X)/len(X).
 
  sum([H|T]) -&gt; H + sum(T);
  sum([]) -&gt; 0.
 
  len([H|T]) -&gt; 1 + len(T);
  len([]) -&gt; 0.
</pre>
<h4>More Patterns</h4>
<pre>
  even([H|T]) when H rem 2 == 0 -&gt;
    [H|even(T)];
  even([_|T]) -&gt;
    even(T);
  even([]) -&gt;
    [].
</pre>
<p>
Finds even numbers by using the first when clause, passing odds to the
second one, which simply calls even on the tail.
</p>
<pre>
  double([H|T]) -&gt; [2*H|double(T)];
  double([]) -&gt; [].
 
  member(H, [H|_]) -&gt; true;
  member(H, [_|T]) -&gt; member(H, T);
  member(_, []) -&gt; false.
</pre>
<p>
Checks the head of the list equality with the element to search for. Fail,
then go through the rest of the list save the head.
</p>
<h4>Accumulators</h4>
<pre>
  average(X) -&gt; average(X,0,0).
 
  average([H|T], Length, Sum) -&gt;
    average(T, Length + 1, Sum + H);
</pre>
<p>
Not sure about this part, he switched slides:
</p>
<pre>
  average([], Length, Sum) -&gt;
    Sum/Length;
</pre>
<h3>Builtin Functions</h3>
<pre>
  63&gt; date().
  {2008,7,22}
  64&gt; time().
  {15,5,32}
  65&gt; length([1,2,3]).
  3
  66&gt; size({4,5,6}).
  3
  67&gt; atom_to_list(asd).
  &quot;asd&quot;
</pre>
<ul>
<li>BIFs can modify realtime properties of the system
 
</li>
</ul>
<h4>Meta Calls</h4>
<pre>
  apply(Module, Function, Args)
 
  68&gt; apply(boolean, b_not, [false]).
  true
  69&gt; apply(boolean, b_not, [true]).
  false
</pre>
<ul>
<li>Dynamically evaluate functions.
 
</li>
<li>Function must be exported
 
</li>
<li>Arguments established at runtime
 
</li>
<li>Extremely powerful when implementing generic code
 
</li>
</ul>
<h2>Creating Processes</h2>
<pre>
  Pid2 = spawn(Mod, Func, Arg)
</pre>
<h2>Message Passing</h2>
<pre>
  Pid ! Msg
</pre>
<ul>
<li>Bang construct (!)
 
</li>
<li>Msg is any valid Erlang type
 
</li>
<li>Goes to the process mailbox, stores the message sequentially
 
</li>
</ul>
<h3>Receive Clause</h3>
<pre>
  receive
    {reset, Board} -&gt; reset(Board);
    {shut_down, Board} -&gt; {error, unknown_msg}
  end
</pre>
<ul>
<li>There is a non-blocking form of receive using an <b>after</b> clause.
 
</li>
</ul>
<h2>Concurrent Programming</h2>
<h2>Process Error Handling</h2>
 
 
 
 
 
 
 
<h2>Classes</h2>
</body>
</html>
Files: 1
Classes: 0
Modules: 0
Methods: 0
Elapsed: 0.138s