Erlang at a glance, IO library

For the past few weeks, I've been writing a bit of code in Erlang. For those who have no idea what Erlang is, it is a declarative functional programming language developed by Ericsson in 1986(it’s that old.) Erlang is a dynamically typed language that's awesome at concurrency, fault tolerance, scalability and that explains its frequent use in the telecom and networking sector.

A few things about Erlang;

  1. All variables start with a capital letter
  2. Variables don’t change
  3. Erlang uses functions, and every function must have an argument
  4. Erlang functions are closed/terminated with a ‘dot’.

An example of a bound variable in Erlang is, let's write a list of atoms.

L = [cow, goats, chicken].

Then we use the lists library to delete one value.

L = lists:delete(cow, L). 
Will generate an error (** exception error: no match of right hand side value [cow,chicken]).

This is because variable L is already taken (bound), the correct code is;

D = lists:delete(cow, L). 

My favorite part, the IO library is pretty straightforward when it comes to Creating, writing and reading files in Erlang. For example to write a basic txt file, in your console write;

{ok, F} = file:open("C:\\Users\\Daniel\\Desktop\\work\\erlang\\text.txt" ,write).
io:format(F, '~s ~n', ['This is my first Erlang text file']).
File:close(F).

Easy, huh? To read the file from console;

{ok, G} = file:open("C:\\Users\\Daniel\\Desktop\\work\\erlang\\text.txt" ,read).
io:get_line(G, "").

Why G, instead of F? F is already bound, remember in Erlang variables are bound to there values. To unbind the variables, all we do is use the function f;

f(F).   %%unbinds F
f(G).   %%unbinds G
f().    %%to unbind all at once

Now, F and G are free variables. Well, this is a really tiny glance into Erlang, I hope you pick up interest in the language.

Written on September 7, 2015