Introducing Erlang
Transcript of Introducing Erlang
Let’s get started
Trying out the Erlang system …
1> 2+3. 5 2> fun (X) -> X+2 end (4). 6 3> lists:map(fun (X) -> X+2 end, [1,2,3,4]). [3,4,5,6]
Using the system
Logging on to the system on windows / raptor
Downloading and using on your own machine
Using the system at Kent
Use PuTTy to ssh connect to raptor, a linux machine.
Raptor password ≠ Kent password
Details of reset etc. on the terminal session worksheet.
Things to remember
You have to end each expression with a full stop and "return" …
1> 2 +3 . 5
Things to remember
You have to end each expression with a full stop and "return" …
1> 2 +3 . 5
You can navigate with the arrow keys …
… up and down between expressions in the history;
… left and right through a particular expression.
Values in Erlang
Numbers
Atoms
Booleans
Tuples and lists
Strings
Functions
Values in Erlang
Numbers
Atoms
Booleans
Tuples and lists
Strings
Functions
Numbers: integers and floats
Integers are “bignums”: arbitrarily large with full precision
8> 1000000*1000000*1000000*1000000*1000000*1000000000*1000000000. 1000000000000000000000000000000000000000000000000 9> lists:foldr(fun(X,Y)->X*Y end,1,[1,2,3,4,5,6,6,7,7,8,8,9,9,10, 11,12,13,14,15,16,17,18,19,20]). 7357095672726159360000
Different bases can be used: base#number
10> 2#100. 4 11> 3#34 * 1: illegal integer
All the usual operators: + - * / div rem
12> 12 div 5. 2 13> 12.0 div 5. ** exception error …
Atoms: “it’s just a piece of data”
An atom is just an atom … it stands for itself
24> foo. foo 25> 'I am an atom'. 'I am an atom'
Atoms can be compared for equality, ordering …
26> foo == 'I am an atom'. false 27> foo==foo. true 29> foo > 'I am an atom'. true
Two special atoms, the booleans: true false
Tuples: putting values together
A tuple has a number of pieces of data
{"Joe","Armstrong",55} {1.2,1.5} {{1.2,1.5},{2.4,5.0}}
A common Erlang idiom is to use the first field to indicate what sort of data is represented by the tuple.
{rectangle,{1.2,1.5},{2.4,5.0}} {circle,{1.1,2.1},3.0}
Typically tuples are heterogenous: different types in different fields.
A tuple is like the set of attributes in a Java object.
Lists: collections of values
A list has a number of pieces of data
["Joe","Armstrong"] [{1.2,1.5}] [{1.2,1.5},{2.4,5.0}] [] [[1,2,3]]
Typically lists are homogenous: all the elements have the same type.
A list is like the collection types in Java.
Tuples and Lists: why?
Both tuples and lists are collections of values, and both can have elements of different types.
["Joe","Armstrong",21] {"Joe","Armstrong",21}
So why do we have both?
The answer is that we can do different things with lists and tuples.
More on this next week and the week after.
Functions
Functions can be data themselves.
30> fun (X) -> X*2 end. #Fun<erl_eval.6.80484245>
For instance, can be arguments of other functions
31> lists:map(fun (X) -> X*2 end, [1,2,3,4]). [2,4,6,8] 32> lists:foldr(fun(X,Y)->X*Y end,1,[1,2,3,4,5,6]). 720
Very powerful data manipulation: map-reduce (Hadoop) and D3 (JS).
More later …
Erlang variables, functions and modules
Understanding variables in Erlang.
Simple function definitions in Erlang.
Erlang modules, compilation and use.
Erlang so far
Erlang works like a calculator …
… computation is expression evaluation.
Values come from these built-in types …
… numbers, atoms, lists, tuples and functions.
Erlang has variables
Erlang has variables …
1> A=2+3. 5 2> B=A-1. 4
Erlang has variables
Erlang has variables …
1> A=2+3. 5 2> B=A-1. 4
… but not as we know it, Jim.
3> A=B+1. 5 4> A=B. ** exception error: no match of right hand side value 4
What's going on?
Erlang has single assignment variables
If a variable doesn't have a value already – it isn't bound … … then ‘=’ gives it a value:
1> A=2+3. 5 2> B=A-1. 4
Erlang has single assignment variables
If a variable doesn't have a value already – it isn't bound … … then ‘=’ gives it a value:
1> A=2+3. 5 2> B=A-1. 4
If it is bound, then ‘=’ is a check on its value: does the value of the RHS match the value of the variable on the LHS?
3> A=B+1. 5 4> A=B. ** exception error: no match of right hand side value 4
So how can we use variables in Erlang?
We can use variables to name values …
1> A=2+3. 5 2> B=A-1. 4
… and then use those variables in defining other values.
5> C={A,B,A}. {5,4,5}
No updateable variables
In Java, we might write something like this …
for(int i = 1; i <= 200; i++) { if(i%2 == 0) { sum = sum + i; } }
We can't do that in Erlang.
... Sum = Sum + i ...
We can build loops … but in a functional way … see below!
Variables in the shell
Erlang has variables.
1> A=2+3. 5 2> B=A-1. 4 3> A=B+1. 5 4> A=B. ** exception error: no match of right hand side value 4
We can – only in the shell – forget bindings …
5> f(A). ok 6> A=B. 4 7> f(). --- forgets all bindings.
Defining functions for ourselves.
The simplest function definitions look like this
double(X) -> times(X,2).
times(X,Y) -> X*Y.
Defining functions for ourselves.
The body can contain multiple expressions: the return value of the function is the value of the last expression in the body:
triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
Modules
These definitions need to go into a module, in the file first.erl
-module(first). -export([double/1,triArea/3]).
double(X) -> times(X,2).
times(X,Y) -> X*Y. triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
-module(first). -export([double/1,triArea/3]).
double(X) -> times(X,2).
times(X,Y) -> X*Y. triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
Modules
-module declaration: the modulefoo lives in the file foo.erl
-module(first). -export([double/1,triArea/3]).
double(X) -> times(X,2).
times(X,Y) -> X*Y. triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
Modules
-export declaration: need to list all the functions that are exported
-module declaration: the modulefoo lives in the file foo.erl
-module(first). -export([double/1,triArea/3]).
double(X) -> times(X,2).
times(X,Y) -> X*Y. triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
Modules
-export declaration: need to list all the functions that are exported
-module declaration: the modulefoo lives in the file foo.erl
Identify a function by its name and itsarity: how many arguments it takes.
-module(first). -export([double/1,triArea/3]).
double(X) -> times(X,2).
times(X,Y) -> X*Y. triArea(A,B,C) -> S = (A+B+C)/2, math:sqrt(S*(S-A)*(S-B)*(S-C)).
Modules
When you call a function from another module, you need to use its fully-qualified name: module:function as in math:sqrt
-export declaration: need to list all the functions that are exported
-module declaration: the modulefoo lives in the file foo.erl
Identify a function by its name and itsarity: how many arguments it takes.
Erlang infrastructure – the basics
Erlang runs on a virtual machine called the BEAM.
Just like the JVM for Java, this makes it Erlang portable, potentially more agile, …
An Erlang module needs to be compiled into BEAM code, before it can be used, e.g. in the Erlang shell, or called from another module.
Once compiled, a file foo.beam is produced for each foo.erl.
Using our functions
The command c(foo). compiles the file foo.erl.
Remember that to use a function … … you need to use fully-qualified names, and … the function must be exported:
1> c(first). {ok,first} 2> double(2). ** exception error: undefined shell command double/1 3> first:double(2). 4 4> first:times(2,3). ** exception error: undefined function first:times/2 5> first:triArea(3,4,5). 6.0
Resourceshttp://learnyousomeerlang.com
Don’t forget …
Terminal sessions start this week.