In this tutorial I’m going to teach the core syntax of the Erlang language with many examples. This video is for someone that already knows another language and wants to translate common programming syntax into Erlang.
If you jump to YouTube in the description you’ll find time stamps to every main part of the video. For best results take notes on the cheat sheet provided below as you watch and leave any questions you have.
If you value videos like this consider donating a $1 on Patreon
[googleplusone]
Code & Cheat Sheet
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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
-module(erlangtut). -export([hello_world/0]). hello_world() -> io:fwrite("hello, world\n"). cd("/Users/derekbanas/Documents/workspace/ErlangTut/src"). c(erlangtut). erlangtut:hello_world(). cd("C:\Users\derekbanas\workspace\ErlangTut\src"). % @author derekbanas % @doc @todo Add description to erlangtut. % ----- BASICS ----- % Modules contain many functions % This will always be the first statement in a file % because it defines the module % We compile to byte code like this c(tut). -module(tut). % Import string functions -import(string, [len/1, concat/2, chr/2, substr/3, str/2, to_lower/1, to_upper/1]). % Defines what functions can be called from this module % /0 defines that this function doesn't receive attributes % /2 defines that add recieves 2 % -export([hello_world/0, add/2, add/3, main/0]). -export([main/0]). % We execute functions like this tut:hello_world(). % fwrite outputs a string to the console hello_world() -> io:fwrite("Hello World\n"). % Recieve 2 values and return the sum add(A, B) -> % You can call other functions % Separate multiple statements with a comma hello_world(), A + B. % You can define functions with the same name % as long as the number of attributes differ add(A, B, C) -> A + B + C. % tut:module_info(). gives you info on your module % ----- VARIABLES ----- % Variable names start with an uppercase letter % or _ and then letters, numbers, _, or @ % A variables value cannot change % A variables type is defined dynamically % You can call a function before it was created % Even if you call multiple functions only % output from the last shows main() -> var_stuff(), atom_stuff(), do_math(5,4), compare(4,4.0), what_grade(10), say_hello(german), string_stuff(), tuple_stuff(), list_stuff(), lc_stuff(), type_stuff(), find_factorial(3), sum([1,2,3]), sum2([1,2,3], 0), for(3,1), map_stuff(), record_stuff(), do_math2(), fun_stuff("Derek"), fun_stuff2(), write_txt("Write to the file"), write_txt2(" More text for the file"), read_txt(), error_stuff(0), read_txt2(), macro_stuff(5,6), spawner(), spawner(), spawner2(50,1), spawner2(100, 51). var_stuff() -> Num = 1, Num. % An Atom is variable thats name equals its value % They start with lowercase letters, or are % surrounded by single quotes atom_stuff() -> 'An Atom'. % ----- MATH ----- do_math(A, B) -> A + B, A - B, A * B, % Float division A / B, % Integer division A div B, % Modulus returns the remainder A rem B, % e raised to the X power math:exp(1), % Natural log math:log(2.71828), % Common log math:log10(1000), % Power math:pow(10,2), % Square root math:sqrt(100), % There is also sin, cos, tan, asin, acos, atan, % sinh, cosh, tanh, asinh, acosh, atanh % Generate random number from 1 to 10 random:uniform(10). % ----- COMPARING VALUES ----- % You receive true or fale from comparisons % 0 isn't equal to false compare(A, B) -> % Check for equality of value and type A =:= B, % Check for inequality of value and type A =/= B, % Disregard type A == B, % Disregard type A /= B, % >, <, >=, =<, and, or, not, xor Age = 18, (Age >= 5) or (Age =< 18). % ----- IF CONDITIONALS ----- % If is used to perform different actions % based on conditions preschool() -> 'Go to preschool'. kindergarten() -> 'Go to kindergarten'. grade_school() -> 'Go to grade school'. what_grade(X) -> if X < 5 -> preschool() ; X == 5 -> kindergarten() ; X > 5 -> grade_school() end. % ----- CASE CONDITIONALS ----- % Case performs different actions based on values say_hello(X) -> case X of french -> 'Bonjour'; german -> 'Guten Tag'; english -> 'Hello' end. % ----- STRINGS ----- % Strings must be surrounded by double quotes string_stuff() -> Str1 = "Random string", Str2 = "Another string", % You can place strings or any data in % output using ~p io:fwrite("String : ~p ~p\n", [Str1, Str2]), % format can be used for similar results Str3 = io_lib:format("It's a ~s and ~s\n", [Str1, Str2]), io:fwrite(Str3), % Get string length len(Str3), % Concatenate strings Str4 = concat(Str1, Str2), Str4, % Get index for character CharIndex = chr(Str4, $n), CharIndex, % Return string start at index and number % of characters Str5 = substr(Str4, 8, 6), Str5, % Get index of string StrIndex = str(Str4, Str2), StrIndex, % All uppercase to_upper(Str1), % All lowercase to_lower(Str1). % ----- TUPLES ----- % A Tuple can hold multiple values tuple_stuff() -> My_Data = {42, 175, 6.25}, % Get all values My_Data, % Store values in another tuple {A,B,C} = My_Data, % Return just 1 value C, % Use an anonymous variable to match a pattern {D,_,_} = My_Data, D, % You can use an Atom as the key for a value % Tagged Tuple My_Data_2 = {height, 6.25}, {height, Ht} = My_Data_2, Ht. % ----- LISTS ----- % Lists contain multiple values from any % data type list_stuff() -> List1 = [1,2,3], List2 = [4,5,6], % Join lists with ++ List3 = List1 ++ List2, List3, % Subtract a list List4 = List3 -- List1, List4, % Retrieve the 1st element (Head) hd(List4), % Retrieve all but the first (Tail) tl(List4), % Add a value to the list with the cons operator List5 = [3|List4], List5, % Get the head and tail [Head|Tail] = List5, Head. % ----- LIST COMPREHENSIONS ----- % List comprehensions make it easy to manipulate lists lc_stuff() -> List1 = [1,2,3], % Multiply every list item times 2 % N is an incrementing list item List2 = [2*N || N <- List1], List2, % You can add conditions like to get only evens List3 = [1,2,3,4], List4 = [N || N <- List3, N rem 2 == 0], List4, % Search through a list of tuples for perfect weather City_Weather = [{pittsburgh, 50}, {'new york', 53}, {charlotte, 68}, {miami, 78}], Great_Temp = [{City, Temp} || {City, Temp} <- City_Weather, Temp >= 50], Great_Temp. % ----- TYPE CONVERSIONS ----- type_stuff() -> % You can check the type of a variable is_atom(name), is_float(3.14), is_integer(10), is_boolean(false), is_list([1,2,3]), is_tuple({height, 6.24}), % You can convert from one to another using % type_to_type % atom_to_binary, atom_to_list, binary_to_atom, % binary_to_list, bitstring_to_list, binary_to_term, % float_to_list, fun_to_list, integer_to_list, % integer_to_list, iolist_to_binary, iolist_to_atom, % list_to_atom, list_to_binary, list_to_bitstring, % list_to_float, list_to_integer, list_to_pid, % list_to_tuple, pid_to_list, port_to_list, ref_to_list, % term_to_binary, term_to_binary, tuple_to_list List1 = integer_to_list(21), List1. % ----- RECURSION ----- % Recursion is the act of a function calling itself % Those things you normally accomplish with looping % in other languages are done using recursion % with Erlang % We can use recursion to find the factorial factorial(N) when N == 0 -> 1; factorial(N) when N > 0 -> N * factorial(N - 1). find_factorial(X) -> Y = factorial(X), io:fwrite("Factorial : ~p\n", [Y]). % 1st: 3 -> 3 * f(2) == 3 * 2 = 6 % 2nd: 2 -> 2 * f(1) == 2 * 1 (Send Above) % 3rd: 1 -> 1 * f(0) == 1 * 1 (Send Above) % Sum a list sum([]) -> 0; sum([H|T]) -> H + sum(T). % sum([1,2,3]) % 1 + sum([2,3]) % 1 + 2 + sum([3]) % 1 + 2 + 3 + sum([]) % Tail Recursive % Instead of holding up the additions % we can keep a running count sum2([], Sum) -> Sum; sum2([H|T], Sum) -> io:fwrite("Sum : ~p\n", [Sum]), sum2(T, H + Sum). % sum2([1,2,3], 0) % sum2([2,3], 1) % sum2([3], 3) % sum2([], 6) % You can create a for loop with recursion for(0,_) -> ok; for(Max,Min) when Max > 0 -> io:fwrite("Num : ~p\n", [Max]), for(Max-1,Min). % for(3,1) Num : 3 % for(2,1) Num : 2 % for(1,1) Num : 1 % for(0,1) End % ----- MAPS ----- % A Map is a group of key value pairs map_stuff() -> Bob = #{f_name=>'Bob', l_name=>'Smith'}, % Get value assigned to a key io:fwrite("1st Name : ~p\n",[maps:get(f_name, Bob)]), % Get all keys io:fwrite("~p\n", [maps:keys(Bob)]), % Get all values io:fwrite("~p\n", [maps:values(Bob)]), % Return everything except the key designated io:fwrite("~p\n", [maps:remove(l_name, Bob)]), % Check if a key exists maps:find(f_name, Bob), % Add a key value to the map maps:put(address, "123 Main", Bob). % ----- RECORDS ----- % You can define a custom type that contains % multiple fields -record(customer, {name = "", bal = 0.00}). record_stuff() -> % Define a customer Sally = #customer{name="Sally Smith", bal=100.00}, % Change data Sally2 = Sally#customer{bal = 50}, % Output data io:fwrite("~p owes $ ~p\n", [Sally2#customer.name, Sally2#customer.bal]). % ----- HIGHER ORDER FUNCTIONS ----- % Functions that can receive other functions as % a parameter is known as a higher order function % Define a function that multiplies % every item by 2 double(X) -> X * 2. % This one triples the number triple(X) -> X * 3. % lists:map takes a function and applies % that function to every item in the list % Fun is used to assign a function % to a variable do_math2() -> lists:map(fun double/1, [1,2,3]), lists:map(fun triple/1, [1,2,3]). % You can use fun to define anonymous functions fun_stuff(N) -> Fun_Stuff = fun() -> io:fwrite("Hello ~p\n",[N]) end, Fun_Stuff(). % You can also access values outside of the function fun_stuff2() -> X = 3, Y = 4, Z = fun() -> io:fwrite("Sum : ~p\n",[X + Y]) end, Z(). % ----- FILE I/O ----- % Create and write to a file write_txt(N) -> % Get file handler and write {ok, Fh} = file:open("MyFile.txt", [write]), % Write the text file:write(Fh, N). write_txt2(N) -> % Get file handler and append {ok, Fh} = file:open("MyFile.txt", [append]), file:write(Fh, N). % Read from file read_txt() -> % Get read permission {ok, File} = file:open("MyFile.txt", [read]), % Read text from file Words = file:read(File, 1024 * 1024), io:fwrite("~p\n", [Words]). % ----- EXCEPTION HANDLING ----- % Exception handling allows our program to % handle errors rather then just crashing % You place code that could crash after % try and then list what happens when certain % errors occur after catch error_stuff(N) -> try Ans = 2 / N, Ans catch error:badarith -> "Can't divide by zero" end. read_txt2() -> try {ok, File} = file:open("MyFile1.txt", [read]), Words = file:read(File, 1024 * 1024), io:fwrite("~p\n", [Words]) catch % This will catch all errors _:_ -> "File Doesn't Exist" end. % ----- MACROS ----- % Macros provide for inline code replacement % Define the constant and what replaces it -define(add(X,Y), {X+Y}). macro_stuff(X,Y) -> % ? defines to use the macro io:fwrite("~p\n", [?add(X,Y)]). % ----- CONCURRENCY ----- % Concurrency is the when several processes % execute at the same time and potentially % interact % As long as the processes aren't dependent % on each other they can run at the same time % Count to 1 million, calculate Pi aren't dependent % but start engine drive car are get_id(M) -> io:fwrite("ID : ~p\n", [M]). % You can generate a process and define the function % to execute. This passes the process ID spawner() -> spawn(fun() -> get_id([self()]) end). % Create a for loop that counts using different % ranges for2(0,_) -> ok; for2(Max,Min) when Max > 0 -> io:fwrite("Num : ~p\n", [Max]), for(Max-1,Min). % The process take turns doing their jobs spawner2(Max, Min) -> spawn(fun() -> for2(Max, Min) end). |
Leave a Reply