introducing erlang
TRANSCRIPT
![Page 2: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/2.jpg)
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]
![Page 3: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/3.jpg)
Using the system
Logging on to the system on windows / raptor
Downloading and using on your own machine
![Page 4: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/4.jpg)
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.
![Page 5: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/5.jpg)
![Page 6: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/6.jpg)
Things to remember
You have to end each expression with a full stop and "return" …
1> 2 +3 . 5
![Page 7: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/7.jpg)
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.
![Page 8: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/8.jpg)
Values in Erlang
Numbers
Atoms
Booleans
Tuples and lists
Strings
Functions
![Page 9: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/9.jpg)
Values in Erlang
Numbers
Atoms
Booleans
Tuples and lists
Strings
Functions
![Page 10: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/10.jpg)
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 …
![Page 11: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/11.jpg)
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
![Page 12: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/12.jpg)
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.
![Page 13: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/13.jpg)
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.
![Page 14: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/14.jpg)
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.
![Page 15: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/15.jpg)
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 …
![Page 16: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/16.jpg)
Erlang variables, functions and modules
Understanding variables in Erlang.
Simple function definitions in Erlang.
Erlang modules, compilation and use.
![Page 17: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/17.jpg)
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.
![Page 18: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/18.jpg)
Erlang has variables
Erlang has variables …
1> A=2+3. 5 2> B=A-1. 4
![Page 19: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/19.jpg)
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?
![Page 20: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/20.jpg)
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
![Page 21: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/21.jpg)
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
![Page 22: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/22.jpg)
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}
![Page 23: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/23.jpg)
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!
![Page 24: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/24.jpg)
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.
![Page 25: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/25.jpg)
Defining functions for ourselves.
The simplest function definitions look like this
double(X) -> times(X,2).
times(X,Y) -> X*Y.
![Page 26: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/26.jpg)
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)).
![Page 27: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/27.jpg)
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)).
![Page 28: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/28.jpg)
-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
![Page 29: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/29.jpg)
-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
![Page 30: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/30.jpg)
-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.
![Page 31: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/31.jpg)
-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.
![Page 32: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/32.jpg)
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.
![Page 33: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/33.jpg)
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
![Page 34: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/34.jpg)
Resourceshttp://learnyousomeerlang.com
![Page 35: Introducing Erlang](https://reader031.vdocument.in/reader031/viewer/2022012516/6190b388bd0c406617737bb9/html5/thumbnails/35.jpg)
Don’t forget …
Terminal sessions start this week.