Control Abstraction
“Abstract” simply means to hide something. In general, we have two classes of abstraction mechanisms in programming languages. They are Control Abstraction and Data Abstraction.
Control Abstraction provide the programmer the ability to hide procedural data.
Data Abstraction allow the definition and use of sophisticated data types without referring to how such types will be implemented.
Control Abstractions in Erlang
Erlang is a functional programming language which means that functions can be used as arguments to functions and that functions can return functions. Functions that manipulate functions are called higher-order functions. The datatype which represent a function in Erlang is called a fun.
Functional Programs can not only manipulate regular data structures but can also manipulate the functions that transform data.
Funs can be used in different was as mentioned below:
1. Performing same operation on every element of a list: To do this we pass funs as argument to functions which is extremely common.
2. Creating our own Control Abstractions: This technique is very useful. Erlang has no for loop. But it gives us a flexibility to create our own for loop. The advantage in creating our own control abstraction is, we can make them to do exactly what we want them to do rather than relying on a predefined set of control abstractions which might not behave as what we want.
3. Implementing things like parser combinators or lazy evaluators: We can write functions which return funs. This is a very powerful technique, but such programs might become difficult to debug.
Defining our own Control Abstractions
As already said, Erlang has no while, for, if statements, we can create one by our self as below:
NOTE: All the programs should be saved with .erl extension.
For Loop:
The declaration export([for/3]) means the function can be called from outside the module for_loop.
Here we have defined a simple for loop. We can use it to make a list of integers or to find the squares of a list of numbers and so on.
To print a list of integers:
Compile the above for_loop.erl file using command c(for_loop).
And then run the program to print a list of numbers from 1 to 20 using for_loop:for(1,20,fun(I) -> I end).
To compute the square of integers from 1 to 10:
Compile the above for_loop.erl file using command c(for_loop).
Get Nivedita Vattipalli’s stories in your inbox
Join Medium for free to get updates from this writer.
And then run the program to print a list of numbers from 1 to 20 using for_loop:for(1,20,fun(I) -> I *I end).
While Loop:
Here we have defined a simple while loop which starts printing from 2. To compile and run the program, execute the commands as below:
Branching
We can use pattern matching in this case. Below example shows how to implement the same:
NOTE: 1. io:format’s formatting is done with the help of tokens being replaced in a string.
2. Character used to denote a token is tilde(~). ~n denotes a linebreak, ~s denotes a string, ~w denotes an integer.
Based on the input of first parameter, branching happens in this case and the appropriate output is displayed. Compile and run the program as follows:
Defining a function:
As already mentioned, Erlang is a functional programming language and hence we would expect to see a lot of emphasis on functions and how they work.
Syntax of a function:
FunctionName(Pattern1… PatternN) ->
body;
Function to multiply two numbers:
Note: 1. The mul() function takes 2 arguments.
2. Here we are defining mul, start functions with export function so that we will be able to use them.
3. We are calling the mul function from start function in this program.
4. We can also call the mul function directly and give the arguments at runtime or use the start function to print the output for already defined input.
Output:
Funs
Funs are used to define anonymous functions in Erlang.
Anonymous Functions
Erlang has an option to define anonymous functions. Anonymous function is a function which has no name associated with it. The anonymous function is defined with fun() keyword.
Output:





