This tutorial
Erlang
- History: Makes it easier to build scalable, fault-tolerant systems
- Functional programming language with dynamic typing
- Processes, node → concurrency, multi-core, distribution
- Tracing and hot code loading
2.1 Setup
Installation
- Windows: http://www.erlang.org/downloads
- Linux/Mac packages from Erlang Solutions
- Linux (usually not the newest):
apt-get install erlang
,yum install erlang
- Mac:
brew install erlang
,port install erlang
- kerl
- for Linux and Mac
- installs Erlang from source
- handles multiple Erlang versions
- bashrc config:
export KERL_DEFAULT_INSTALL_DIR=$HOME/.kerl/installations/ export KERL_ENABLE_PROMPT=true
Editor/IDE: Emacs, Vim, IntelliJ IDEA, Visual Studio Code, Sublime, ...
The Shell: werl.exe
(Windows), erl
(anything else)
Shell Commands:
- Up/down (history), ctrl-a (home), ctrl-e (end)
- Terminate expressions with a period (.)
- Quit:
q()
orctrl-c a
Numbers: +
, -
, *
, /
, div
, rem
Invariable Variables
One = 1
- Variables start with a capital letter
=
: assignment (it actually does pattern matching)- We don't change variables; we bind them
f()
,f(Var)
: forget variables in the shell
Atoms: atom
, 'Special atom'
Boolean Algebra & Comparison operators
true
,false
=:=
,=/=
Tuples
Point = {4, 5}.
element(1, Point).
3.3 Modules
What are modules: lists:seq(1, 4).
Module declaration: see useless.erl
Compiling the code
> c(useless).
> useless:greet_and_add_two(-3).
> useless:module_info().
Typing Functions
- See useless.erl
edoc:run(["useless.erl"], []). % Produces useless.html.
Install Erlang.
Étude 1: Getting Comfortable with Erlang.
Étude 2: Functions and Modules.
Use the shell to calculate how many months did the "ISS One Year Long Mission" take, considering that it took 342 days. (If you) are stuck: first calculate the length of the average month.)
Extra 2: Write functions temp:f2c(F)
and temp:c2f(C)
which convert between
centigrade and Fahrenheit scales.
(Source.)
If you want to convert Celsius to Fahrenheit, just multiply the temperature by 1.8 and then add 32 to the product. If you want to convert temperature from Fahrenheit to Celsius, subtract 32 from the number and then divide the difference by 1.8.
Don't forget to add type specs and docstrings.
-
Write the function
time:hms_to_seconds(H, M, S)
that converts a time duration given as hours:minutes:seconds into seconds. E.g. -
Implement the following inverse functions:
time:seconds_to_h(Seconds) time:seconds_to_m(Seconds) time:seconds_to_s(Seconds)
The following must hold for all non-negative integers:
time:hms_to_seconds(time:seconds_to_h(Seconds), time:seconds_to_m(Seconds), time:seconds_to_s(Seconds)) =:= Seconds
-
Implement the inverse function
time:seconds_to_hms(Seconds)
, which returns a{H, M, S}
tuple. The following must hold for all non-negative integers:H = element(1, time:seconds_to_hms(Seconds)), M = element(2, time:seconds_to_hms(Seconds)), S = element(3, time:seconds_to_hms(Seconds)), time:hms_to_seconds(H, M, S) =:= Seconds
Don't forget to add type specs and docstrings.
Watch "Erlang: The Movie". It's only 11 minutes: