+ All Categories
Home > Documents > Introducing Erlang

Introducing Erlang

Date post: 14-Nov-2021
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
35
Introducing Erlang Simon Thompson [email protected] CO545 Lecture 2
Transcript
Page 1: Introducing Erlang

Introducing Erlang

Simon [email protected]

CO545 Lecture 2

Page 2: 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]

Page 3: Introducing Erlang

Using the system

Logging on to the system on windows / raptor

Downloading and using on your own machine

Page 4: Introducing Erlang

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
Page 6: Introducing Erlang

Things to remember

You have to end each expression with a full stop and "return" …

1> 2 +3 . 5

Page 7: Introducing Erlang

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

Values in Erlang

Numbers

Atoms

Booleans

Tuples and lists

Strings

Functions

Page 9: Introducing Erlang

Values in Erlang

Numbers

Atoms

Booleans

Tuples and lists

Strings

Functions

Page 10: Introducing Erlang

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

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

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

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

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

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

Erlang variables, functions and modules

Understanding variables in Erlang.

Simple function definitions in Erlang.

Erlang modules, compilation and use.

Page 17: Introducing Erlang

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

Erlang has variables

Erlang has variables …

1> A=2+3. 5 2> B=A-1. 4

Page 19: Introducing Erlang

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

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

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

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

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

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

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

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

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

-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

-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

-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

-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

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

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

Resourceshttp://learnyousomeerlang.com

Page 35: Introducing Erlang

Don’t forget …

Terminal sessions start this week.


Recommended