Getting started with erlang

This post is more of notes as compared to a step-by-step tutorial.

Install Erlang
Download and install Erlang http://erlangcentral.org/downloads/

Shell

$ erl
Erlang/OTP 17 [erts-6.0] [source-07b8f44] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.0  (abort with ^G)

erl is the command to start the Erlang shell.

You can also use this online interpreter: http://www.erlangcentral.com/interpreter/index.yaws

Numbers
Expressions in Erlang are terminated by .(dot) followed by whitespace (line break, space etc.)

Usage of arithmetic operators (+, *, /, div, rem and -):

1> 5+5.
10
2> 5*10.
50
3> 25/2.
12.5
4> 25 div 2.
12
5> 25 rem 2.
1
6> 10-5.
5
  • ‘/’ operator by default performs a floating point operation.
  • For integer division, use ‘div’ operator.
  • For modulo operation, use ‘rem’ operator.

Numbers in other bases

Numbers can be expressed with base other than 10.

7> 2#101.
5
8> 8#723.
467
9> 16#abe.
2750

Variables

  • Being a functional programming language, variables are immutable.
  • Variable names must start with a capital letter or ‘_’.
10> FirstVariable.
* 1: variable 'FirstVariable' is unbound
11> Three.
* 1: variable 'Three' is unbound
12> Three=3.
3
13> Five=Three+2.
5
14> Two=2.
2
15> Five=Three+Two.
5
16> Five=Five+1.
** exception error: no match of right hand side value 6
17> Three=4.
** exception error: no match of right hand side value 4
  • A variable once assigned a value cannot be changed. (Refer command 16 and 17).
  • A variable can be assigned the same value any number of times. (Refer command 13 and 15).
  • ‘=’ operator tries to compare the values on left and right side. If there is a variable on left hand side and it is not bound, it is assigned the value.
  • If left and right side both have value and do not match, it throws an error.
18> 85=45+40.
85
19> 85=40+40.
** exception error: no match of right hand side value 80

More examples on variables:

20> three=3.
** exception error: no match of right hand side value 3
21> _=3.
3
22> _.
* 1: variable '_' is unbound
  • ‘three’ is invalid variable name as it does not start with capital letter.
  • _ is a special variable to which no value can be bound.

Erase a variable’s value

23> Three.
3
24> f(Three).
ok
25> Three.
* 1: variable 'Three' is unbound
26> Five.
5
27> Two.
2
28> f().
ok
29> Two.
* 1: variable 'Two' is unbound
30> Five.
* 1: variable 'Five' is unbound

Atoms

Atoms are the reason why variable names cannot start with lower-case letters. Atoms are literals,constants with the value same as their name. As the name suggests, it cannot be smashed into species nor can it be changed.

Well, ironman is ‘ironman’ and it can’t be ‘Tony Stark’ 🙂

An atom that does not start with lower-case letter or contains anything other than alphanumeric characters, “_” or “@” must be enclosed in single quotes (‘).

31> iamatom.
iamatom
32> iamatom=5.
** exception error: no match of right hand side value 5
33> iamatom='iamatom'.
iamatom
34> iamatom=iamatom.
iamatom
35> 'I am also an atom'.
'I am also an atom'
  • Atoms generally represent constants and should be used with care as they are not garbage-collected.
  • They are referred in ‘atom table’ with memory usage of 4 bytes/atom in 32-bit and 8 bytes/atom in 64-bit system.
  • Reserved atoms: after and andalso band begin bnot bor bsl bsr bxor case catch cond div end fun if let not of or orelse query receive rem try when xor.

Boolean Algebra and comparison operators

Well, what is the use of a language if it can’t distinguish apple to oranges 🙂

1> true and false.
false
2> true or false.
true
3> true and true.
true
4> false or false.
false
5> not (true and true).
false
6> true xor false.
true
7> true xor true.
false
8> false xor true.
true
9> false xor false.
false
10> true andalso true.
true
11> false andalso true.
false
12> true orelse false.
true
  • and, or, not, xor are quite straight forward.
  • andalso is similar to and. The difference is that it does not evaluate another expression if it does not need to.
  • orelse is counterpart of or as andalso is for and.

Euality and Inequality:

13> One=1.
1
14> Two=2.
2
15> 2=:= 2.
true
16> 2=/=2.
false
17> 5=/=2.
true
18> One=:=One.
true
19> One=/=Two.
true
  • =:= is comparable to ‘==’ in other languages and =/= is comparable to ‘!=’
  • ‘==’ and ‘/=’ in Erlang are used to compare values of different types.
20> 5=:=5.0.
false
21> 5==5.0.
true
22> 5=/=5.0.
true
23> 5/=5.0.
false

Other comparison operators:

24> 1<2.
true
25> 1>2.
false
26> 1>=2.
false
27> 1<=2.
* 1: syntax error before: '<='
  • Oh!! Does this mean there is no ‘less than or equal to’ operator? Here it is with a different symbol:
27> 1=<2.
true
28> 2=<2.
true

Well, do you think you have got it all correct? Let’s try this:

29> 7-foo.
** exception error: an error occurred when evaluating an arithmetic expression
in operator -/2
called as 7 - foo
30> 5=:=false.
false
  • Erlang slapped you hard with the error details when you subtracted foo from 7.
  • But, it is all okay comparing 5 to false!!!! – Well, 5 is not false, right?

If this is not enough, take this:

31> 1<false.
true
32> 2<false.
true
33> 54<false.
true
34> 546734<false.
true

A total ordering on all data types is defined based on which numbers < atoms. Hence, 1<false. ‘false’ is not a special value. It is just an atom, remember?

Well, we have had enough now. Let’s take a break and we will continue from here.

1 thought on “Getting started with erlang

Leave a comment