25
Erlang variables, functions and modules Simon Thompson [email protected] CO545 Lecture 3

Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Embed Size (px)

Citation preview

Page 1: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Erlang variables, functions and modules

Simon [email protected]

CO545 Lecture 3

Page 2: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Values in Erlang

Numbers 71820809342981189182192

Atoms atom 'i am an atom' true

Booleans false true

Tuples and lists {[1,2,3],{1,2},true}

Strings "atom" "i am an atom"

Functions fun (X,Y) -> X*Y end

Page 3: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Erlang variables, functions and modules

Understanding variables in Erlang.

Simple function definitions in Erlang.

Erlang modules, compilation and use.

Page 4: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 5: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Erlang has variables

Erlang has variables …

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

Page 6: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 7: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 8: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 9: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 10: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 11: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 12: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Defining functions for ourselves.

The simplest function definitions look like this

double(X) -> times(X,2).

times(X,Y) -> X*Y.

Page 13: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 14: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 15: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

-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 16: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

-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 17: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

-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 18: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

-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 19: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 20: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

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 21: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Making a choice: pattern matching

Suppose that we want to make a choice in defining a function

is_zero(0) -> true; is_zero(X) -> false.

Instead of writing (just) a variable as an argument, we can write a literal (or more complicated things … see below).

Page 22: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Making a choice: pattern matching

Boolean or usually allows both possibilities to be true.

“You can choose pizza or pasta …”

In a café, you can have pizza or pasta, but not both: this is called or is exclusive or. How can we define exclusive or on booleans.?

xOr(true,false) -> true; xOr(false,true) -> true; xOr(X,Y) -> false.

Page 23: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

A common idiom: “don’t care”

In the final case here, X and Y aren't used in the definition …

… we don’t case about their values, as this is the catch all case, and we can replace them with _ …

xOr(true,false) -> true; xOr(false,true) -> true; xOr(_,_) -> false.

Page 24: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Making a choice: pattern matching

You can also use pattern matching to check equality between (parts of) patterns. In this case between two arguments:

xOr(X,X) -> false; xOr(_,_) -> true.

Page 25: Erlang variables, functions and modules - University of Kentsjt/co545/Lecture3.pdf · Erlang variables, functions and modules Understanding variables in Erlang. Simple function definitions

Erlang variables, functions and modules

Understanding variables in Erlang.

Simple function definitions in Erlang.

Erlang modules, compilation and use.

Simple pattern matching.