Comparing
version 5 and
version 4 backh1. Erlang Study
Origin: "Getting Started With Erlang":http://erlang.org/doc/doc-5.5.2/doc/getting_started/part_frame.html
h2. The Erlang Shell
You have to tell it you are done entering code by finishing the line with a full stop "." and a carriage return
h2. Modules and Functions
In general module and the file name need to be the same. So inside _tut.erl_ write:
-module(tut1).
-export([fac/1]).
fac(1) ->
1;
fac(N) ->
N * fac(N - 1).
The two parts of the fac function are called its *clauses*.
We end the first part with a ";" which indicates that there is more of this function to come. the second part ends with a "." saying that there are no more parts of this function.
h2. Atoms
*Atoms* start with a small letter (*manual*), for example: charles, centimeter, inch. Atoms are simply names, nothing else. They are not like variables which can have a value.
-module(tut2).
-export([convert/2]).
convert(M, inch) ->
M / 2.54;
convert(N, centimeter) ->
N * 2.54.
9> c(tut2).
{ok,tut2}
10> tut2:convert(3, inch).
1.18110
11> tut2:convert(7, centimeter).
17.7800
h2. Tuples
Erlang has a way to group things together to make things more understandable. We call these *tuples*. Tuples are surrounded by "{" and "}".
-module(tut3).
-export([convert_length/1]).
convert_length({centimeter, X}) ->
{inch, X / 2.54};
convert_length({inch, Y}) ->
{centimeter, Y * 2.54}.
14> c(tut3).
{ok,tut3}
15> tut3:convert_length({inch, 5}).
{centimeter,12.7000}
16> tut3:convert_length(tut3:convert_length({inch, 5})).
{inch,5.00000}
We have shown tuples with two parts above, but tuples can have as many parts as we want and contain any valid Erlang *term*.
Tuples have a fixed number of things in them. We call each thing in a tuple an *element*. So in the tuple {moscow,{c,-10}}, element 1 is moscow and element 2 is {c,-10}.
h2. Lists
Lists in Erlang are surrounded by "[" and "]".
A very useful way of looking at parts of lists, is by using "|".
18> [First |TheRest] = [1,2,3,4,5].
[1,2,3,4,5]
19> First.
1
20> TheRest.
[2,3,4,5]
*Note: Variables starting with CAPITAL LETTER. Variable can only be given a value once in its context (scope).*
In general we can say we *use tuples where we would use "records" or "structs" in other languages* and we *use lists when we want to represent things which have varying sizes*, (i.e. where we would use linked lists in other languages).
Erlang does not have a string date type, instead strings can be represented by lists of ASCII characters.
31> [97,98,99].
"abc"
h2. Writing Output to a Terminal
The function _format/2_ (i.e. format with two arguments) takes two lists. The first one is nearly always a list written between " ". This list is printed out as it stands, except that each *~w* is replaced by a term taken in order from the second list. Each *~n* is replaced by a new line.
The _io:format/2_ function itself *returns the atom ok* if everything goes as planned.
32> io:format("hello world~n", []).
hello world
ok
33> io:format("this outputs two Erlang terms: ~w ~w~n", [hello, world]).
this outputs two Erlang terms: hello world
ok
*Note: A comment starts with a '%' character and goes on to the end of the line.*
h2. Guards
...
list_max([Head|Rest], Result_so_far) when Head > Result_so_far ->
list_max(Rest, Head);
...
_when_ is a special word we use before the _->_ in the function to say that we should only use this part of the function if the test which follows is true. We call tests of this type a *guard*. If the guard isn't true (we say the guard fails), we try the next part of the function.
Some useful operators in guards are, '<' less than, '>' greater than, '==' equal, '>=' greater or equal, '<=' less or equal, '/=' not equal.