<commit_before>---
title: "Erlang: My First functional programming Language"
date: "2017-04-26 11:34:13"
categories: [erlang]
tags: [erlang]
---

Erlang is a general purpose, concurrent functional programming language.I have attended two online courses from futurelearn[futurelearn_link]. The first course mainly focussed on the basic of the Erlang Language and the second course was into concurrent programming in Erlang. The Erland Language is approx 30years old and getting popularity these days due to its wide usage in concurrent environment. The Erlang was orginally designed for improving the development in telephony applications, where thery have to handle a lot of active connections at the same time. Another problem that lead to the development of Erlang is the need for availability and fault tolerance. By 1988 Erlang proved that it was suitable for telephony applications. In 1992 the work for erlang virtual machine BEAM was started( earlier versions of erlang was implemented in Prolog which was found slower hence BEAM). In 1998 Ericsson announced they were able to acheive high availability of nine 9's using Erlang circuit switched digital telephone exhanges manufactured by Ericsson.



<h3>SOME OF THE FEATURE OF ERLANG (JOE ARMSTRONG)</h3>

In Erlang everything is a process.
Process are strongly isolated.
Process creation and destruction are light weight.
Message passing is the only way for processes to interact.
Process has unique names.
If you know the nme of process you can send message to it.
Processes share no resources.
Error-handling is non-local.
Processes do what they are supposed to do else they fail.
[from wiki][wikipedia_link_features]

With all this in mind lets move forward and allow me to write the challenges faced and the new experiences gained during the said courses.

<h3>Course 1: Basic Programming In Erlang.</h3>

<h4>Week 1:</h4>

  The first week of the course was mainly focussed on Erlang basics. One of the interesting aspect of Erlang that I came to know is the Pattern Matching. In Erlang variables are bind to values using pattern matching, uses single assignment. For example 

{% highlight erlang %}

Eshell V8.2  (abort with ^G)
1> A=5.
5
2> A.
5
3> A=6.
** exception error: no match of right hand side value 6
{% endhighlight %}


  In the above example , A is bounded to value 5. If we try to bind it again(step 3) we will get an exception. Another thing to note is that the varibales must start with Capital letter otherwise it will become an atom. [Atoms][atoms_link_stackoverflow] are immutables, a literal ,a constant with a name.

  Functions:
    A simple function can be defined like this,

{% highlight erlang %}

-module(isZero).
-export([is_zero/1]).

is_zero(0)->
 true;
is_zero(X)->
 false.
 {% endhighlight %}
 
    The function is_zero checks whether a given value is zero or not. If first clause holds we will get true else false. The clauses are separated by a semi-colon(;) and the final clause have a period(.) at the end. We have a module name isZero and a export list. In export list is_zero/1 says that is_zero is a function which will take a single argument. Every  function should be exported to use outside the module. The function vcan be executed after compiling the file isZero.erl(same as the module name) as shown below. This function in action,


```erlang
    
1>c(isZero).  %% Compiling
{ok, isZero}
2> isZero:is_zero(0). %% Accessing module:function/arity
true
3> isZero:is_zero(3).
false
```

    Recursion
      Another different thing I saw in Erlang was that there is no explicit control structures like for , while loops etc. We have to attain looping with recursion alone. An example of factorial problem,

```erlang
    
fact(0)->
    1;
fact(N)->
    fact(N-1)*N.

``` 
we can implement this with Tail Recursion as



```erlang

fact(N)->
    fac(N,1).

fac(0, Acc)->
    Acc;
fac(N, Acc) when N > 0 ->
    fac(N-1, Acc*N).

```


   Difference between Recursion & Tail Recursion [go_here][learn_you_some_erlang_link_recursion]. This Article explains tail recursion in a fantastic way.
    
    A while loop implementation:<commit_msg>functional<commit_after>---
title: "Erlang: My First functional programming Language"
date: "2017-04-26 11:34:13"
categories: [erlang]
tags: [erlang]
---

Erlang is a general purpose, concurrent functional programming language.I have attended two online courses from futurelearn[futurelearn_link]. The first course mainly focussed on the basic of the Erlang Language and the second course was into concurrent programming in Erlang. The Erland Language is approx 30years old and getting popularity these days due to its wide usage in concurrent environment. The Erlang was orginally designed for improving the development in telephony applications, where thery have to handle a lot of active connections at the same time. Another problem that lead to the development of Erlang is the need for availability and fault tolerance. By 1988 Erlang proved that it was suitable for telephony applications. In 1992 the work for erlang virtual machine BEAM was started( earlier versions of erlang was implemented in Prolog which was found slower hence BEAM). In 1998 Ericsson announced they were able to acheive high availability of nine 9's using Erlang circuit switched digital telephone exhanges manufactured by Ericsson.



<h3>SOME OF THE FEATURE OF ERLANG (JOE ARMSTRONG)</h3>

In Erlang everything is a process.
Process are strongly isolated.
Process creation and destruction are light weight.
Message passing is the only way for processes to interact.
Process has unique names.
If you know the nme of process you can send message to it.
Processes share no resources.
Error-handling is non-local.
Processes do what they are supposed to do else they fail.
[from wiki][wikipedia_link_features]

With all this in mind lets move forward and allow me to write the challenges faced and the new experiences gained during the said courses.

<h3>Course 1: Basic Programming In Erlang.</h3>

<h4>Week 1:</h4>

  The first week of the course was mainly focussed on Erlang basics. One of the interesting aspect of Erlang that I came to know is the Pattern Matching. In Erlang variables are bind to values using pattern matching, uses single assignment. For example 

{% highlight erlang %}

Eshell V8.2  (abort with ^G)
1> A=5.
5
2> A.
5
3> A=6.
** exception error: no match of right hand side value 6
{% endhighlight %}


  In the above example , A is bounded to value 5. If we try to bind it again(step 3) we will get an exception. Another thing to note is that the varibales must start with Capital letter otherwise it will become an atom. [Atoms][atoms_link_stackoverflow] are immutables, a literal ,a constant with a name.

  Functions:
    A simple function can be defined like this,

{% highlight erlang %}

-module(isZero).
-export([is_zero/1]).

is_zero(0)->
 true;
is_zero(X)->
 false.
 {% endhighlight %}
 
   The function is_zero checks whether a given value is zero or not. If first clause holds we will get true else false. The clauses are separated by a semi-colon(;) and the final clause have a period(.) at the end. We have a module name isZero and a export list. In export list is_zero/1 says that is_zero is a function which will take a single argument. Every  function should be exported to use outside the module. The function vcan be executed after compiling the file isZero.erl(same as the module name) as shown below. This function in action,


{% highlight erlang %}
    
1>c(isZero).  %% Compiling
{ok, isZero}
2> isZero:is_zero(0). %% Accessing module:function/arity
true
3> isZero:is_zero(3).
false

{% endhighlight %}

Recursion
      Another different thing I saw in Erlang was that there is no explicit control structures like for , while loops etc. We have to attain looping with recursion alone. An example of factorial problem,

```erlang
    
fact(0)->
    1;
fact(N)->
    fact(N-1)*N.

``` 
we can implement this with Tail Recursion as



```erlang

fact(N)->
    fac(N,1).

fac(0, Acc)->
    Acc;
fac(N, Acc) when N > 0 ->
    fac(N-1, Acc*N).

```


   Difference between Recursion & Tail Recursion [go_here][learn_you_some_erlang_link_recursion]. This Article explains tail recursion in a fantastic way.
    
    A while loop implementation:
{'fraction_non_alphanumeric': 0.06306414731014562, 'fraction_numerical': 0.015404982549043206, 'mean_word_length': 4.098159509202454, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4332690', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2220, 'n_words': 1217}