<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Erlang for Python programmers</title>
<meta name="version" content="S5 1.0" />
<link rel="stylesheet" href="ui/slides.css" type="text/css" media="projection" id="slideProj" />
<link rel="stylesheet" href="ui/opera.css" type="text/css" media="projection" id="operaFix" />
<link rel="stylesheet" href="ui/print.css" type="text/css" media="print" id="slidePrint" />
<script src="ui/slides.js" type="text/javascript"></script>
</head>
<body>

<div class="layout">

<div id="currentSlide"></div>
<div id="header"></div>
<div id="footer">
<h1>SoCal Piggies, July 13 2006</h1>
<h2>Erlang for Python programmers</h2>
<div id="controls"></div>
</div>

</div>


<div class="presentation">

<div class="slide">
<h1>Erlang for Python programmers</h1>
<h3>Daniel Arbuckle</h3>
<h4>SoCal Piggies</h4>
</div>


<div class="slide">
<h1>What is Erlang?</h1>
<ul>
<li>A language designed by Ericsson Computer Science Laboratory (yes, the telephone company)</li>
<li>Designed from the get-go for concurrency programming</li>
<li>Scalable from simple concurrency to distributed execution</li>
<li>Functional (but that might not mean what you think)</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Digression: Functional Programming</h1>
<ul>
<li>"Functional" doesn't mean "it works." Functional means "like a mathematical function."</li>
<li>In math, a function takes one or more inputs and produces a single output (mathematicians, please don't kill me).</li>
<li>The same is true in Erlang. Functions transform one or more inputs into an output, and <em>that's all they do.</em></li>
<li>That has some implications that may not be immediately obvious to you:
         <ul>
         <li>Functions can't have side-effects, so you can't set state variables</li>
         <li>In fact, variables can only be set once in a namespace; you can't reassign them new values</li>
         <li>Functions don't care about their context, so code is very re-usable</li>
         </ul>
</li>
<li>Side-effects are allowed when they take place outside the 'universe' of the program: this allows I/O routines.</li>
<li>Functional Programming is strongly advocated here: http://www.math.chalmers.se/~rjmh/Papers/whyfp.pdf</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Why would we want that?</h1>
<ul>
<li>The mathematical approach can help keep concurrent programs sane, and cut down on 'emergent properties' of the software</li>
<li>Not being able to modify variables makes local concurrency much more efficient; this benefit applies doubly to distributed concurrency</li>
<li>There's a beauty to it that's rewarding in itself</li>
<li>. . . after your head has finished exploding</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Digression complete</h1>
<ul>
<li>Now we're going to look at how to do some fundamental things in Erlang:</li>
<li>Loops</li>
<li>Branching</li>
<li>Message passing</li>
<li>Error handling</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Loops in Erlang and Python</h1>
<table>
<tr>
<td>Where a Python programmer would type:</td>
<td>an Erlang programmer would type:</td>
</tr>
<tr>
<td>
<pre>
def foo(i):
    for x in range(i, 11):
        do_something(x)
</pre>
</td>
<td>
<pre>
foo(10) ->
    do_something(0).

foo(I) when I < 10 ->
    do_something(I),
    foo(I + 1).
</pre>
</td>
</tr>
</table>
<ul>
<li>In both cases, do_something is invoked (10 - i + 1) times</li>
<li>Recursion is used in Erlang because it's the only way to write code that refers to a variable whose value actually varies over time (technically, it's a different variable with each invocation)</li>
<li>When the last expression in a function is a recursive call to the same function (a "tail recursion"), Erlang optimizes that expression into a loop in the generated code. It's a recursion only in concept.</li>
<li>Capitol I is used for the variable name because all variables names must start with capitol letters. Words starting with a lowercase letter are atoms (basically, a string without quotes). </li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Branching in Erlang and Python</h1>
<table>
<tr>
<td>Where a Python programmer would type:</td>
<td>an Erlang programmer would type:</td>
<td>or:</td>
</tr>
<tr>
<td>
<pre>
def foo(i):
    if i == 10:
        do_something(i)
    elif i < 10:
        do_something(i)
        foo(i + 1)
</pre>
</td>
<td>
<pre>
foo(10) ->
    do_something(0).

foo(I) when I < 10 ->
    do_something(I),
    foo(I + 1).
</pre>
</td>
<td>
<pre>
foo(I) ->
    if
        I == 10 ->
            do_something(I);
        I < 10 ->
            do_something(I),
            foo(I + 1)
    end.
</pre>
</td>
</tr>
</table>
<ul>
<li>On the previous slide, I glossed over the <code>foo(10)</code> and <code>when I < 10</code>. These are the two primary ways of implementing branching in Erlang.</li>
<li>All of the functions with the same name and the same number of parameters ("arity") are implicitly wrapped into a single function and a sequence of if/elif blocks.</li>
<li>A parameter matches when it is either an unbound variable, or when it is equal to the passed value. When all of the parameters match and the 'when' clause, if any, is true, that version of the function is executed.</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Message passing in Erlang and Python</h1>
<table>
<tr>
<td>Where a Python programmer would type:</td>
<td>an Erlang programmer would type:</td>
</tr>
<tr>
<td>
<pre>
sock.sendall(pickle.dumps(('hello', 19, x)))
f = sock.makefile('r')
word, number, x = pickle.load(f)
f.close()
if word == 'welcome':
    process(word, number, x)
else:
    error(word, number x)
</pre>
</td>
<td>
<pre>
Proc_id ! {hello, 19, X}.
receive
    {welcome, Number, Y} ->
        process(welcome, Number, Y);
    {Word, Number, Y} ->
        error(Word, Number, Y)
end.
</pre>
</td>
</tr>
</table>
<ul>
<li>These examples aren't totally equivalent. Each time a receive block executes, it finds the first message in the message queue that matches one of its clauses, instead of just considering the first message.</li>
<li>Erlang matches receive clauses in the same way that it does function parameters.</li>
<li>A receive block will block until a message it understands enters the queue</li>
<li>Nonlinear message delivery does a lot to decouple Erlang processes from each other</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Error handling in Erlang and Python</h1>
<table>
<tr>
<td>Where a Python programmer would type:</td>
<td>an Erlang programmer would type:</td>
</tr>
<tr>
<td>
<pre>
try:
    something()
except ByTheGodsNooo:
    recover()
</pre>
</td>
<td>
<pre>
process_flag(trap_exit, true),
Risky = spawn(thismodule, something, []),
link(Risky),
receive
    {'EXIT', Risky, byTheGodsNoo} ->
        recover();
    {'EXIT', Risky, Reason} ->
        exit(Reason)
end.

</pre>
</td>
</tr>
</table>
<ul>
<li>In Erlang, you spawn off a new process to do anything risky. If the process terminates normally, you know it worked.</li>
<li>This creates a relationship between processes similar to the Python call stack, allowing errors to propogate until something can handle them.</li>
<li>If the trap_exit process flag is unset, the calling process would be automatically terminated, just as unhandled exceptions terminate execution of a function in Python.</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<div class="slide">
<h1>Other tidbits</h1>
<ul>
<li>Erlang has a match operator '='. It performs a function fairly similar to function parameter matching. For example <code>{A, {B, 10}} = something()</code> would bind A and B to the values in the equivalent positions in the return value of something(), so long as the 10 matched the value in the equivalent position.</li>
<li>Erlang's per-process and process switching overheads are extremely low.</li>
<li>Erlang is freely downloadable from http://www.erlang.org/</li>
<li>Erlang's concurrency model is very similar to C. A. R. Hoare's mathematical theory of Communicating Sequential Processes: http://www.usingcsp.com/</li>
<li>Erlang has a fully capable macro facility, which can be used to define new semantics.</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>

<!--
<div class="slide">
<h1>[slide title]</h1>
<ul>
<li>[point one]</li>
<li>[point two]</li>
<li>[point three]</li>
<li>[point four]</li>
<li>[point five]</li>
</ul>
<div class="handout">
[any material that should appear in print but not on the slide]
</div>
</div>
-->

</div>

</body>
</html>