Archive for March 2011

Introduction to Erlang : Basic Types (2/2)

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

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

Read the rest of this entry »

Introduction to Erlang : Basic Types (1/2)

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

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 »

Introduction to Erlang : Typing

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

In this post I will present you the data typing in Erlang.

Dynamic Typing

Erlang is a dynamic typing programming language. That means that when “declaring” a variable you do not need to statically specify the type it will be (as in static typing languages). Read more about dynamic typing here. For example, this is how you would declare and initialize an integer in C (static typing):

int i = 17;

while in Erlang you would do: (we will see how to declare variables in a while)

I = 17.

This approach has both advantages and disadvantages. For example, when programming, it is fast and convenient not to declare the variables’ types, but in big projects it can lead to code readability problems unless well documented.

Variable Declaration

Erlang is heavily influnced by Prolog, so Prolog programmers should be ready to discover several commonalities. As with Prolog, the variable declaration is implicit; every character sequence generated by the regular expression [A-Z_]([0-9a-zA-Z_]*) is a variable. Some examples are A, Ab, A_b, Ab1, _, _A_b3. Just to mention that the ones starting with “_” has a special meaning; they are “don’t care” variables. When you use them, the value they will take will probably not be used later. Something like place keepers. You will meet them again and again, so don’t worry for now.

Variable Assignement

Another importand characteristic that Erlang inherited from Prolog is binding with pattern matching. In a nutshell, a value is not assigned to a variable, but bound with pattern matching. The most important consequence of this is that variables in Erlang are single assignement. Once bound to a value, their value cannot change for their lifetime. For example, in an emulator try the following:

1> A = 10.
10
2> A = 11.

The output you will get is:

** exception error: no match of right hand side value 11

What happens is that A is bound to the value 10, so Erlang tries to pattern match 10 with the value 11, which is impossible.

Next

Since I want to keep the posts rather small, I will break this one here and I will present the actual Erlang types on the following post(s).

Windows from version 1 to version 7..

Windows 1

Windows 1

In a worth to see video, created by Andrew Tait, the installation/update and compatibility of Windows from version 1 to 7 is explored. The installation were done using VMWare and the outcome was that Microsoft actually did a very good job on keeping the systems compatible! Just watch it!

Opera Software makes fun of Apple’s Mac App Store’s weird decision

Opera Logo
Opera was the first non-native browser to be available for download in Apple’s Mac App Store. Though, Apple judged that the Opera browser is “inappropriate” for people younger than 17 years old.

Opera Software “accepted” this weird decision with a humorous/satiric press release:

This week, the Opera web browser became the first non-native browser made available in Apple’s Mac App Store, but only for those over seventeen years of age. Jan Standal, VP of Desktop Products for Opera Software, is surprised.

“I’m very concerned,” says Standal. “Seventeen is very young, and I am not sure if, at that age, people are ready to use such an application. It’s very fast, you know, and it has a lot of features. I think the download requirement should be at least 18.”

For those under 17, there is a workaround. Just visit www.opera.com and download it. We do not ask for your age or your credit card number. Please, get your parents’ permission before using this browser.

Structures and Memory Consumption in C

Motivation

The way you design you structures in C can have a (significant) impact on the size they occupy in memory!

Memory structure of a structure

A structure occupies contiguous space in memory. Though, in order to provide efficient access to the structure’s fields, C uses padding. In other words, the fields are aligned to the memory words.

Example

In a system with:

  • word : 4 bytes
  • char : 1 byte
  • int : 4 bytes

without padding would be:

char (8bit) int (32bit)

and since the word size is 4 bytes the integer would belong to 2 different words, so in order to access it, two memory accesses would be necessary.
In order to avoid it, the actual memory organization is:

char (8bit) padding (24bit) int (32bit)

The Possible Problem

A bad designed structure can possible occupy more space than needed.

Example

Take a look at the following structure:

struct bad {
  char c1;
  int i1;
  char c2;
};

One would possibly say that since 1 + 4 + 1 = 6 bytes is the total size of the fields, 2 words = 8 bytes will be needed to store it. This is not true. Due to the padding we have:

c1 (8bit) padding (24bit) i1 (32bit) c2 (8bit) padding (24bit)

3 words = 12 bytes, 4 more bytes than necessary.

Solution

Just take into account how the types fit together:

struct good {
  char c1; //c1 + c2 = 2 bytes
  char c2;
  int i1;
};

This design needs the minimum number of 2 words = 8 bytes.

Outcome

The bigger the structure gets, the more space you can lose.. So, take care of it while designing the structures!

Introduction to Erlang : Installing Erlang

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

About Erlang

Erlang is a functional programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

The initial open-source Erlang release contains the implementation of Erlang, as well as a large part of Ericsson’s middleware for building distributed high-availability systems.
Read the rest of this entry »