hank / life
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
5c16509
commit 5c1650968f8c7fc96686c2c7f1d394daccce33c6
tree b91d7dcd1a313382010f78e1ec5bca4515f7c545
parent 7aa244dc7b478f429c3acad67e5a46784adad651
tree b91d7dcd1a313382010f78e1ec5bca4515f7c545
parent 7aa244dc7b478f429c3acad67e5a46784adad651
| e31c7a27 » | Erik | 2008-07-22 | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
| 2 | <html> | ||||
| 3 | <head> | ||||
| 4 | <title>RDoc Documentation</title> | ||||
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> | ||||
| 6 | </head> | ||||
| 7 | <body> | ||||
| 8 | <h2>File: Practical.Erlang.rdoc</h2> | ||||
| 9 | <table> | ||||
| 10 | <tr><td>Path:</td><td>Practical.Erlang.rdoc</td></tr> | ||||
| 11 | <tr><td>Modified:</td><td>Tue Jul 22 16:11:39 -0700 2008</td></tr> | ||||
| 91cc8fcd » | Erik | 2008-07-23 | 12 | </table> | |
| e31c7a27 » | Erik | 2008-07-22 | 13 | ||
| 14 | <h1>OSCON 2008, Tutorial 4: Practical Erlang</h1> | ||||
| 15 | <p> | ||||
| 16 | This tutorial is not about the history of Erlang, comparing Erlang to other | ||||
| 17 | languages, or a comprehensive overview. | ||||
| 18 | </p> | ||||
| 19 | <p> | ||||
| 20 | The goal is that we can read and write erlang, understand the concurrency | ||||
| 21 | model and the error handling model. | ||||
| 22 | </p> | ||||
| 23 | <h2>Basic Erlang</h2> | ||||
| 24 | <h3>Starting the system</h3> | ||||
| 25 | <pre> | ||||
| 26 | erl | ||||
| 27 | </pre> | ||||
| 28 | <p> | ||||
| 29 | You get some *1>* and *2>* stuff. End everything with a . | ||||
| 30 | </p> | ||||
| 31 | <h3>Data Types</h3> | ||||
| 32 | <h4>Integers</h4> | ||||
| 33 | <ul> | ||||
| 34 | <li>10 | ||||
| 35 | |||||
| 36 | </li> | ||||
| 37 | <li>-234 | ||||
| 38 | |||||
| 39 | </li> | ||||
| 40 | <li>16#16 = conversion from hex | ||||
| 41 | |||||
| 42 | </li> | ||||
| 43 | <li>2#101010 = conversion from binary | ||||
| 44 | |||||
| 45 | </li> | ||||
| 46 | <li>$A = ASCII value of A | ||||
| 47 | |||||
| 48 | </li> | ||||
| 49 | </ul> | ||||
| 50 | <p> | ||||
| 51 | There‘s also floats. | ||||
| 52 | </p> | ||||
| 53 | <h4>Atoms</h4> | ||||
| 54 | <p> | ||||
| 55 | Constant literals, always lowercase letter or in quotes. | ||||
| 56 | </p> | ||||
| 57 | <h4>Tuples</h4> | ||||
| 58 | <p> | ||||
| 59 | Fixed number of elements, any size, and contain any valid Erlang | ||||
| 60 | expression. | ||||
| 61 | </p> | ||||
| 62 | <h4>Lists</h4> | ||||
| 63 | <ul> | ||||
| 64 | <li>Variable number of elements | ||||
| 65 | |||||
| 66 | </li> | ||||
| 67 | <li>dynamic size | ||||
| 68 | |||||
| 69 | </li> | ||||
| 70 | <li>strings are lists of characters | ||||
| 71 | |||||
| 72 | </li> | ||||
| 73 | <li>lists are encapsulated by *[]* | ||||
| 74 | |||||
| 75 | </li> | ||||
| 76 | <li>empty string is an empty list | ||||
| 77 | |||||
| 78 | </li> | ||||
| 79 | </ul> | ||||
| 80 | <h5>Concatenation</h5> | ||||
| 81 | <pre> | ||||
| 82 | [1,2,3] ++ [4,5,6] | ||||
| 83 | </pre> | ||||
| 84 | <h4>Complex Data Structures</h4> | ||||
| 85 | <ul> | ||||
| 86 | <li>Just a big list generally, containing tuples. | ||||
| 87 | |||||
| 88 | </li> | ||||
| 89 | <li>Uses a garbage collector. | ||||
| 90 | |||||
| 91 | </li> | ||||
| 92 | </ul> | ||||
| 93 | <h3>Pattern Matching</h3> | ||||
| 94 | <ul> | ||||
| 95 | <li>Pattern = Expression | ||||
| 96 | |||||
| 97 | </li> | ||||
| 98 | </ul> | ||||
| 99 | <h4>Assignment</h4> | ||||
| 100 | <pre> | ||||
| 101 | {B,C,D} = {10,foo,bar}. | ||||
| 102 | [H|T] = [1,2,3] | ||||
| 103 | </pre> | ||||
| 104 | <h4>Testing</h4> | ||||
| 105 | <pre> | ||||
| 106 | {A,A,B} = {abc,def,123}. | ||||
| 107 | </pre> | ||||
| 108 | <p> | ||||
| 109 | Fails because A is being reassigned | ||||
| 110 | </p> | ||||
| 111 | <pre> | ||||
| 112 | [A,B,C,D] = [1,2,3] | ||||
| 113 | </pre> | ||||
| 114 | <p> | ||||
| 115 | Fails because the lists are not the same size | ||||
| 116 | </p> | ||||
| 117 | <pre> | ||||
| 118 | [A,B|C] = [1,2,3,4,5,6,7] | ||||
| 119 | </pre> | ||||
| 120 | <p> | ||||
| 121 | Suceeds, A=1, B=2, and C=the rest | ||||
| 122 | </p> | ||||
| 123 | <pre> | ||||
| 124 | [H|T] = [] | ||||
| 125 | </pre> | ||||
| 126 | <p> | ||||
| 127 | Fails since at least 1 element is needed on the right | ||||
| 128 | </p> | ||||
| 129 | <h3>Variables</h3> | ||||
| 130 | <pre> | ||||
| 131 | {A,_, [B|_], {B}} = {abc,23,[22,x], {22}} | ||||
| 132 | </pre> | ||||
| 133 | <p> | ||||
| 134 | Succeeds - A=abc, B=22 | ||||
| 135 | </p> | ||||
| 136 | <h3>Functions</h3> | ||||
| 137 | <ul> | ||||
| 138 | <li>You have to define functions with the correct arity. | ||||
| 139 | |||||
| 140 | </li> | ||||
| 141 | <li>Need to be exported to be used outside | ||||
| 142 | |||||
| 143 | </li> | ||||
| 144 | </ul> | ||||
| 145 | <h2>Sequential Erlang</h2> | ||||
| 146 | <h3>Conditionals</h3> | ||||
| 147 | <h4>Case</h4> | ||||
| 148 | <pre> | ||||
| 149 | case lists:member(foo,List) of | ||||
| 150 | true -> ok; | ||||
| 151 | false -> {error, unknown} | ||||
| 152 | end | ||||
| 153 | </pre> | ||||
| 154 | <ul> | ||||
| 155 | <li>No punctuation on last <b>case</b> clause. | ||||
| 156 | |||||
| 157 | </li> | ||||
| 158 | <li>Can evaluate one or more expressions per clause, seperated by ;. | ||||
| 159 | |||||
| 160 | </li> | ||||
| 161 | <li>There‘s a dont-care clause that can protect you from a runtime error. | ||||
| 162 | |||||
| 163 | </li> | ||||
| 164 | </ul> | ||||
| 165 | <h4>If</h4> | ||||
| 166 | <pre> | ||||
| 167 | if | ||||
| 168 | X<1 -> smaller; | ||||
| 169 | X>1 -> greater; | ||||
| 170 | X==1 -> equal | ||||
| 171 | end | ||||
| 172 | </pre> | ||||
| 173 | <ul> | ||||
| 174 | <li>true guard allows a default case. It‘s not mandatory. | ||||
| 175 | |||||
| 176 | </li> | ||||
| 177 | </ul> | ||||
| 178 | <h4>Guards</h4> | ||||
| 179 | <p> | ||||
| 180 | We can use guards in functions using <b>when</b>. | ||||
| 181 | </p> | ||||
| 182 | <pre> | ||||
| 183 | factorial(N) when N > 0 -> | ||||
| 184 | N * factorial(N-1); | ||||
| 185 | factorial(0) -> 1. | ||||
| 186 | </pre> | ||||
| 187 | <p> | ||||
| 188 | Since everything is pattern matched sequentially, we get a performance | ||||
| 189 | improvement by having the N>0 case first. | ||||
| 190 | </p> | ||||
| 191 | <pre> | ||||
| 192 | is_number | ||||
| 193 | is_atom | ||||
| 194 | length | ||||
| 195 | X == Y % Simply compares values | ||||
| 196 | X =:= Y % Also compares data types | ||||
| 197 | </pre> | ||||
| 198 | <p> | ||||
| 199 | All guards have to succeed here: | ||||
| 200 | </p> | ||||
| 201 | <pre> | ||||
| 202 | swap(String) when is_list(String), | ||||
| 203 | length(String) < 10 -> ... | ||||
| 204 | </pre> | ||||
| 205 | <p> | ||||
| 206 | Only one guard has to succeed here: | ||||
| 207 | </p> | ||||
| 208 | <pre> | ||||
| 209 | swap(Tuple) when is_tuple(Tuple); | ||||
| 210 | is_atom(Tuple) -> ... | ||||
| 211 | </pre> | ||||
| 212 | <h3>Recursion</h3> | ||||
| 213 | <h4>Traversing lists</h4> | ||||
| 214 | <pre> | ||||
| 215 | average(X) -> sum(X)/len(X). | ||||
| 216 | |||||
| 217 | sum([H|T]) -> H + sum(T); | ||||
| 218 | sum([]) -> 0. | ||||
| 219 | |||||
| 220 | len([H|T]) -> 1 + len(T); | ||||
| 221 | len([]) -> 0. | ||||
| 222 | </pre> | ||||
| 223 | <h4>More Patterns</h4> | ||||
| 224 | <pre> | ||||
| 225 | even([H|T]) when H rem 2 == 0 -> | ||||
| 226 | [H|even(T)]; | ||||
| 227 | even([_|T]) -> | ||||
| 228 | even(T); | ||||
| 229 | even([]) -> | ||||
| 230 | []. | ||||
| 231 | </pre> | ||||
| 232 | <p> | ||||
| 233 | Finds even numbers by using the first when clause, passing odds to the | ||||
| 234 | second one, which simply calls even on the tail. | ||||
| 235 | </p> | ||||
| 236 | <pre> | ||||
| 237 | double([H|T]) -> [2*H|double(T)]; | ||||
| 238 | double([]) -> []. | ||||
| 239 | |||||
| 240 | member(H, [H|_]) -> true; | ||||
| 241 | member(H, [_|T]) -> member(H, T); | ||||
| 242 | member(_, []) -> false. | ||||
| 243 | </pre> | ||||
| 244 | <p> | ||||
| 245 | Checks the head of the list equality with the element to search for. Fail, | ||||
| 246 | then go through the rest of the list save the head. | ||||
| 247 | </p> | ||||
| 248 | <h4>Accumulators</h4> | ||||
| 249 | <pre> | ||||
| 250 | average(X) -> average(X,0,0). | ||||
| 251 | |||||
| 252 | average([H|T], Length, Sum) -> | ||||
| 253 | average(T, Length + 1, Sum + H); | ||||
| 254 | </pre> | ||||
| 255 | <p> | ||||
| 256 | Not sure about this part, he switched slides: | ||||
| 257 | </p> | ||||
| 258 | <pre> | ||||
| 259 | average([], Length, Sum) -> | ||||
| 260 | Sum/Length; | ||||
| 261 | </pre> | ||||
| 262 | <h3>Builtin Functions</h3> | ||||
| 263 | <pre> | ||||
| 264 | 63> date(). | ||||
| 265 | {2008,7,22} | ||||
| 266 | 64> time(). | ||||
| 267 | {15,5,32} | ||||
| 268 | 65> length([1,2,3]). | ||||
| 269 | 3 | ||||
| 270 | 66> size({4,5,6}). | ||||
| 271 | 3 | ||||
| 272 | 67> atom_to_list(asd). | ||||
| 273 | "asd" | ||||
| 274 | </pre> | ||||
| 275 | <ul> | ||||
| 276 | <li>BIFs can modify realtime properties of the system | ||||
| 277 | |||||
| 278 | </li> | ||||
| 279 | </ul> | ||||
| 280 | <h4>Meta Calls</h4> | ||||
| 281 | <pre> | ||||
| 282 | apply(Module, Function, Args) | ||||
| 283 | |||||
| 91cc8fcd » | Erik | 2008-07-23 | 284 | 68> apply(boolean, b_not, [false]). | |
| 285 | true | ||||
| 286 | 69> apply(boolean, b_not, [true]). | ||||
| 287 | false | ||||
| 288 | </pre> | ||||
| e31c7a27 » | Erik | 2008-07-22 | 289 | <ul> | |
| 290 | <li>Dynamically evaluate functions. | ||||
| 291 | |||||
| 292 | </li> | ||||
| 293 | <li>Function must be exported | ||||
| 294 | |||||
| 295 | </li> | ||||
| 296 | <li>Arguments established at runtime | ||||
| 297 | |||||
| 298 | </li> | ||||
| 299 | <li>Extremely powerful when implementing generic code | ||||
| 300 | |||||
| 301 | </li> | ||||
| 302 | </ul> | ||||
| 303 | <h2>Creating Processes</h2> | ||||
| 91cc8fcd » | Erik | 2008-07-23 | 304 | <pre> | |
| 305 | Pid2 = spawn(Mod, Func, Arg) | ||||
| 306 | </pre> | ||||
| 307 | <h2>Message Passing</h2> | ||||
| 308 | <pre> | ||||
| 309 | Pid ! Msg | ||||
| 310 | </pre> | ||||
| 311 | <ul> | ||||
| 312 | <li>Bang construct (!) | ||||
| 313 | |||||
| 314 | </li> | ||||
| 315 | <li>Msg is any valid Erlang type | ||||
| 316 | |||||
| 317 | </li> | ||||
| 318 | <li>Goes to the process mailbox, stores the message sequentially | ||||
| 319 | |||||
| 320 | </li> | ||||
| 321 | </ul> | ||||
| 322 | <h3>Receive Clause</h3> | ||||
| 323 | <pre> | ||||
| 324 | receive | ||||
| 325 | {reset, Board} -> reset(Board); | ||||
| 326 | {shut_down, Board} -> {error, unknown_msg} | ||||
| 327 | end | ||||
| 328 | </pre> | ||||
| 329 | <ul> | ||||
| 330 | <li>There is a non-blocking form of receive using an <b>after</b> clause. | ||||
| 331 | |||||
| 332 | </li> | ||||
| 333 | </ul> | ||||
| 334 | <h2>Concurrent Programming</h2> | ||||
| e31c7a27 » | Erik | 2008-07-22 | 335 | <h2>Process Error Handling</h2> | |
| 336 | |||||
| 337 | |||||
| 338 | |||||
| 339 | |||||
| 340 | |||||
| 341 | |||||
| 342 | |||||
| 343 | <h2>Classes</h2> | ||||
| 344 | </body> | ||||
| 345 | </html> | ||||
| 346 | Files: 1 | ||||
| 347 | Classes: 0 | ||||
| 348 | Modules: 0 | ||||
| 349 | Methods: 0 | ||||
| 350 | Elapsed: 0.138s | ||||
| 91cc8fcd » | Erik | 2008-07-23 | 351 | ||
