/  31
 
Thinking in Erlang
A
GUIDE TO FUNCTIONAL PROGRAMMING IN
E
RLANG FOR THEEXPERIENCED PROCEDURAL DEVELOPER
Robert Baruch
autophile@zoominternet.netVersion 0.9.1
February 5, 2007
 
Thinking in Erlang RIGHT HEADER HERE
Copyright
This work is licensed under the Creative Commons Attribution-Share Alike 2.5 License. Toview a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/ or send a letterto Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.2
 
Thinking in Erlang RIGHT HEADER HERE
Contents
1 Introduction 4
1.1 What this document is not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 What this document is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 Porting code to Erlang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4 Hello, World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.5 Compiling and running hello.erl . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Scope of variables 7
2.1 No global state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2 Dealing with no global state . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Matching 9
3.1 Basic matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Function argument matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.3 If and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Loops 14
4.1 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.2 Less painful loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174.3 Summary of less painful loops . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 Processes 19
5.1 Doing two things at once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.2 Interprocess communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205.3 Process termination reasons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.4 Processes as objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225.5 Generic servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245.6 Distributed Erlang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255.7 Synchronization not necessary . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
6 Dealing with errors 26
6.1 Let it fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266.2 Workers and supervisors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
References 31
3

Share & Embed

More from this user

Recent Readcasters

Add a Comment

Characters: ...

Jeremy LeBardleft a comment

What is this comment attached too?

autophileleft a comment

In Figures 13 and 14, there is a bug: factorial(0) -> 1. should be factorial(0) -> 1; factorial(0, Acc) -> Acc, factorial(N, Acc) ->... needs to be factorial(0, Acc) -> Acc; factorial(N, Acc) ->...

abestoleft a comment

This is exactly what I needed, thanks a lot! Also, if you (the author) ever read this, how about replacing 'RIGHT HEADER HERE' with something more descriptive? ;)