Integers
10
-234
16#AB10F
2#110111010
$A
Floats
17.368
-56.654
12.34E-10.
- B#Val is used to store numbers in base
< B >.
- $Char is used for ascii values
(example $A instead of 65).
Back to top
abcef
start_with_a_lower_case_letter
'Blanks can be quoted'
'Anything inside quotes \n\012'
- Indefinite length atoms are allowed.
- Any character code is allowed within an
atom.
Back to top
{123, bcd}
{123, def, abc}
{person, 'Joe', 'Armstrong'}
{abc, {def, 123}, jkl}
{}
- Used to store a fixed number of items.
- Tuples of any size are allowed.
Back to top
[123, xyz]
[123, def, abc]
[{person, 'Joe', 'Armstrong'},
{person, 'Robert', 'Virding'},
{person, 'Mike', 'Williams'}
]
"abcdefghi"
becomes - [97,98,99,100,101,102,103,104,105]
""
becomes - []
- Used to store a variable number of items.
- Lists are dynamically sized.
- "..." is short for the list of integers
representing the ascii character codes of the
enclosed within the quotes.
Back to top
Complex Data Structures
[{{person,'Joe', 'Armstrong'},
{telephoneNumber, [3,5,9,7]},
{shoeSize, 42},
{pets, [{cat, tubby},{cat, tiger}]},
{children,[{thomas, 5},{claire,1}]}},
{{person,'Mike','Williams'},
{shoeSize,41},
{likes,[boats, beer]},
...
- Arbitrary complex structures can be cre-
ated.
- Data structures are created by writing
them down (no explicit memory alloca-
tion or deallocation is needed etc.).
- Data structures may contain bound vari-
ables.
Back to top
Pattern Matching
A = 10
Succeeds - binds A to 10
{B, C, D} = {10, foo, bar}
Succeeds - binds B to 10, C to foo and D
to bar
{A, A, B} = {abc, abc, foo}
Succeeds - binds A to abc, B to foo
{A, A, B} = {abc, def, 123}
Fails
[A,B,C] = [1,2,3]
Succeeds - binds A to 1, B to 2, C to 3
[A,B,C,D] = [1,2,3]
Fails
Back to top
Pattern Matching (Cont)
[A,B|C] = [1,2,3,4,5,6,7]
Succeeds - binds A = 1, B = 2,
C = [3,4,5,6,7]
[H|T] = [1,2,3,4]
Succeeds - binds H = 1, T = [2,3,4]
[H|T] = [abc]
Succeeds - binds H = abc, T = []
[H|T] = []
Fails
{A,_, [B|_],{B}} = {abc,23,[22,x],{22}}
Succeeds - binds A = abc, B = 22
- Note the use of "_", the anonymous (don't
care) variable.
Back to top
Function Syntax
Is defined as a collection of clauses.
func(Pattern1, Pattern2, ...) ->
... ;
func(Pattern1, Pattern2, ...) ->
... ;
...
func(Pattern1, Pattern2, ...) ->
... .
Evaluation Rules
- Clauses are scanned sequentially until a match is found.
- When a match is found all variables occurring in the head
become bound.
- Variables are local to each clause, and
are allocated and deallocated automatically.
- The body is evaluated sequentially.
Back to top
Functions (cont)
-module(mathStuff).
-export([factorial/1, area/1]).
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
area({square, Side}) ->
Side * Side;
area({circle, Radius}) ->
% almost :-)
3 * Radius * Radius;
area({triangle, A, B, C}) ->
S = (A + B + C)/2,
math:sqrt(S*(S-A)*(S-B)*(S-C));
area(Other) ->
{invalid_object, Other}.
Back to top