Introduction to Erlang : Control Flow
Control Flow
As we saw in the previous post, pattern matching with different function clauses can be used in order to control the execution flow in Erlang. Erlang also provides the if, case, and receive control flow constructs that can be used in a function body. In this post I will only present the if and casestatements since receive is used for message passing and I will write a dedicated post about the subject. Both if and case are similar to the equivalent statements of other programming languages.
if statement
The format of an if statement in Erlang is the following:
if Boolean_Expression1 -> If_body1; Boolean_Expression2 -> If_body2; ... true -> If_body_cath_all end |
So the different clauses, except the last one, are like else if in other languages, while the last one (true ->) is like the else; it succeeds when all the previous clauses have failed.
Read the rest of this entry »
Introduction to Erlang : Declaring Functions
Functions
As you know by now, Erlang is a functional programming language. As this suggests, functions are the basic “ingredient” of an Erlang program. In my point of view, different programming paradigms pose different problem solving philosophy:
- Procedural: describe the steps needed to be taken to solve the problem
- Object-orientation: design the objects that will lead you to the solution
- Logical (Declarative): describe the problem properly and let the language solve it
- Functional: define small and precise functions that alltogether solve the problem
With this in mind, lets continue on how to declare a function (in a module).
Examples
While introducing functions, I will use several examples that implement list functions, although there are built-ins (BIFs) that implement the same functionality. The reason I will do so is that most of these functions are small, easy to understand, and operate on lists; one of the most, if not the most, important type in Erlang.
Declaring a Function
A simple function declaration has the following format:
function_name(Argument1, Argument2, ...) -> Statement1, Statement2, ... . |
Where a statement can be another function call, an assignement, a comparison, a control statement (if for example), or a statement called for its side effects.
Read the rest of this entry »
Opera Dragonfly : Opera’s Web Development Tool
Some time ago Opera Software announced that they would open-source their web development tool, called Dragonly. On the 14th of March Opera Software announced the first beta version of Dragonly after it became open-source. This release came few days before the Opera 11.10 Beta Browser release. Dragonfly is a Firebug-like tool that aims to aid the web developer and designer on the development and debugging process.
Technologies
Dragonly provides support for the latest web technologies, such as HTML5 APIs and SVG files. I will present you the Dragonfly capabilities later on the screenshot tour.
Installation
Opera comes with Dragonly pre-installed. In order to use the beta version do the following:
- In the Opera’s address bar type
opera:config#DeveloperTools|DeveloperToolsURL, or else typeopera:configand search for the keyword “develop“. - Replace the “Developer Tools URL” with
https://dragonfly.opera.com/app/cutting-edge/(it washttps://dragonfly.opera.com/app/). - Use
Ctrl+Shift+Ito start Dragonfly. You can alternatively started byright click -> Inspect Elementon a page.
Screenshot Tour
Opera 11.10 Beta : A First Look
Today Opera released the Opera version 11.10 Beta version, codenamed Barracuda. I installed it and gave it a first look. Here I present you the new stuff that I recognized.
Redesigned Speedial
This looks as the biggest (and almost the only) change done regarding the user interface.
Although it is an interesting redesign that gives the ability to have as many speedial shortcuts as wanted, I used to use a 3×3 speedial which now seems to fit a little worse in my screen.
An interesting ability is the one of changing the shortcuts’ positions by drag and drop.
Closed-Tabs Can Flashing
Whenever you close a tab in Opera it goes in the closed tabs can on the upper-right. You can use the can in order to reopen a closed tab, ability which is very very useful! Now, the can is flashing whenever a new tab is added (aka closed).
Visual “help”
I noticed that some visual aids for the “novice” users were added, in order to help them get accustomed with some more advanced capabilities of the browser. For example, when you first install the browser and type something in the address bar, you get a message that informs you that you can perform a search straight from the address bar.
Javascript Performance
Before installing Opera 11.10 Beta, I ran the SunSpider 0.91 Javascript benchmark (Opera 11.01 – Ubuntu). The total result I got was 726.7ms +/- 15.5%. With Opera 11.10, under the same conditions (same tabs open, no use while benchmarking, and same OS load) I got a total result of 1003.6ms +/- 0.5%. Probably it is because of some ongoing work being done on the Javascript engine.
Overall
I did not have the time to test the other “big” feature of Barracuda (the ability to install missing plugin, such as Adobe flash, easier), but, in my point of view, what I saw is not enough to explain a new release. I hope the final 11.10 release will have more surprises for Opera Browser’s users.
Introduction to Erlang : Modules & Compilation
Modules
A module is a container for functions; it provided the contained functions with a common namespace. Modules are used to organize functions in Erlang. Usually, a program in Erlang spans over more than one modules. You can imagine a module as a package in Java, or a header file in C.
Calling a Function
The calling convention in Erlang is module:function(argument1, argument2, ...). For example:
1> lists:max([1,3,2]). 3 |
Defining Modules
Lets say we want to create a module that will contain our own implementation of list functions and name it mlists.
Read the rest of this entry »
Selecting a random file/folder of a folder in Linux
I always had the following problem: “How to select which movie (from the ones in my collection) to watch“. In the past, I had used the random (pseudo-random to be precise) capabilities of C, Java, Erlang, and Javascript to select the movie.
Today I wrote a simple script that selects a random content (file or folder) within a folder:
#! /bin/sh if [ $# -gt 0 ]; then cd "$1"; fi ls -1 | awk 'BEGIN {srand()} {x[NR] = $0} END {print "Selected", x[1 + int(rand() * NR)]}' |
Save it in a file (lets say srandom, make it executable:
$ chmod +x srandom |
and execute it either with 0 or 1 arguments. Without an argument, the selection is done from the contents of the srandom‘s container, else the path given as an argument is used.
$ ./srandom.sh Selected lrandom.html $ ./srandom.sh /bin Selected gzexe |
Update
A much more elegant solution:
$ ls -1 | shuf -n1 |
Introduction to Erlang : Basic Types (2/2)
More Data Types
Today we will see some more sophisticated Erlang’s data types; tuple, list, and fun.
Tuple
Tuple is a compound data type; it consists of elements of any data type. A tuple has the form:
{Element1, Element2, ..., ElementN} |
where N is called the size the tuple.
1> {1,2}. {1,2} 2> {true, {value, whatever}}. {true,{value,whatever}} 3> {1, a, 1.1, {{{{4}}}}}. {1,a,1.1,{{{{4}}}}} 4> is_tuple({}). % built-in function (BIF) true 5> element(3, {1,2,3}). % the element at position 3 3 6> size({1,2,3}).% the size of the tuple 3 |
Introduction to Erlang : Basic Types (1/2)
Command Terminator
Erlang uses a simple dot (.) as the command terminator. Consequently every correct Erlang statement should terminate with a dot.
1> 1 1> 2. * 2: syntax error before: 2 1> 1. 1 2> 2. 2 |
The Most Basic Types
I will introduce the data types of Erlang and the basic operations on them by example. Most of the material presented today will look familiar to other programming languages. The next post will present some more sophisticated data types.
Read the rest of this entry »

