Archive for March 2011

Introduction to Erlang : Recursion (1/2)

This entry is part 9 of 16 in the series Introduction to Erlang

Recursion

The definition of the word recursion is “(mathematics) an expression such that each term is generated by repeating a particular mathematical operation”, according to the WordNet. Recursion is one of the most powerful “tools” in a functional programming language and so it is for Erlang. Recursion can be used to apply divide and conquer techniques to problem solving, where a problem is broken to smaller subproblems, the subproblems are solved, and the results are “merged” to generate the final result.

Recursion happens when a function’s body definition includes a call to the function itself.

functionA(...) ->
    Body_before_recursion, % optional
    functionA(...),
    Body_after_recursion. % optional

Recursion is used instead of the conventional loop statements of other programming languages, such as while and for in C.
Read the rest of this entry »

Functional instead of Object Oriented Programming at CMU

Recently, Robert Harper, a Professor of Computer Science at Carnegie Mellon University (CMU) posted about the new introductory CS curriculum at CMU.

Two new courses has been introduced and one more is planned. The first two are about Functional and Imperative Programming respectively. The third one will be a course on Data Structures and Algorithms.

At the same time, Object-Oriented programming is removed from the introductory curriculum, because it is considered both anti-modular and anti-parallel, thus unsuitable for a CS curriculum.

The Standard ML programming language will be used for the Data Structures and Algorithms course. SML was also my first functional programming language while taking the Programming Languages I course during my Diploma. Although it is a “clean” and easy to go language, I would prefer a more inherently parallel language to be used instead. Erlang could be a good candidate!

I vote for this change :-). This shift “against” Object-Oriented programming is a natural one since Parallel Programming is a necessity in the Multi-core era.. An old joke (which is actually stolen from a joke about regular expressions) says:

You have to solve a problem. The problem has certain performance requirements. You decide to use Java and threads.. You have to solve two problems.

Introduction to Erlang : Control Flow

This entry is part 8 of 16 in the series Introduction to Erlang

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

This entry is part 7 of 16 in the series Introduction to Erlang

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:

  1. In the Opera’s address bar type opera:config#DeveloperTools|DeveloperToolsURL, or else type opera:config and search for the keyword “develop“.
  2. Replace the “Developer Tools URL” with https://dragonfly.opera.com/app/cutting-edge/ (it was https://dragonfly.opera.com/app/).
  3. Use Ctrl+Shift+I to start Dragonfly. You can alternatively started by right click -> Inspect Element on a page.

Screenshot Tour

Read the rest of this entry »

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.

Opera 11.11 Beta Screenshot

Opera 11.11 Beta Screenshot


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.
Drag and Drop Shortcuts

Drag and Drop Shortcuts

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

This entry is part 6 of 16 in the series Introduction to Erlang

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