Upload
tranhuong
View
222
Download
3
Embed Size (px)
Citation preview
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
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
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).
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.
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.
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.
Erlang variables, functions and modules
Understanding variables in Erlang.
Simple function definitions in Erlang.
Erlang modules, compilation and use.
Simple pattern matching.