Strings

06/05/2009

You can define a String using “,

Name = “Simone”.

Note: You have to use double quotes, not single ones!

A string in Erlang is just a List of integer that represent characters that compose the string you defined.
If we try to do:

[Head | RemainingString] = Name.

We will get Head -> 83 (the ‘S’) and RemainingString -> “imone”.

How to know that 83 means ‘S’? You can get $<letter>. to see the value, for example:

$S. -> 83 (so we know that 83 means ‘S’)

So using this thing we can compose a list of integer (representing letters) and create a string like:

[$S, $i, $m, $o, $n, $e]. -> “Simone”

Note: these values are related to Latin-1 (ISO-8859-1) character codes!

Advertisements

List are (as name implies) a list of things (mixed type) defined using [] separating items with ,
For example:

[simone, marco, 2+3, {language, erlang}].

returns:

[simone,marco,5,{language,erlang}]

The “head of the list” is the first item in the list (‘simone’ in our example), when you remove the head from a list the remaining part is called “tail of the list” ([marco,5,{language,erlang}] in our example). So a simple schema for a list is [head|tail].

A list:

Devices = [iphone, mac, arduino].

Another list created using an existing one:

ToBuy = [mouse, keyboard | Devices].

Now let’s try to pull out the head of the list using the matching operator ‘=’!

[FirstThingToBuy | OtherThingsToBuy] = ToBuy.

Now I have:

FirstThingToBuy -> mouse (the ex head of the list)

and:

OtherThingsToBuy -> [keyboard,iphone,mac,arduino] (the tail of the list)

Now we need to buy another item (the second), so:

[SecondThingToBuy | OtherThingsToBuy1] = OtherThingsToBuy.

Now I have:

SecondThingToBuy -> keyboard (the new ex hed of the list)

and:

OtherThingsToBuy1 -> [iphone,mac,arduino] (the new tail of the list)

Note: I had to use OtherThingsToBuy1 instead of OtherThingsToBuy because OtherThingsToBuy had already a value and we know that we can’t change it! (I don’t still like it …)

One thing I had clear about erlang (see my first post) is that in erlang a variable gets a value and never change it, it looks a bit different in reality.

When you execute something like X = 5. Erlang read ‘=’ as a matching operator, “X must be equal to 5”, so to become true the phrase it sets X the value of 5.

So … if you have a tuple like:

Person = {person, simone, 26}.

you can make something like:

{person, Name, Age} = Person.

In that way you will have Name with ‘simone’ as value and Age with ’26’. Good!

Another try:

Person = {person,
{name, simone},
{age, 26},
{location, italy}}.

To get the name you have to:

{_,{_,Who},_,_} = Person.

‘_’ is called anonymous variable and it represents variables not interesting for us. Notice that the structure of the first operand is the same of the Person, it simply has many ‘_’ where there is non interesting data.

Another example:

Desk = {desk,
{material, plastic},
{height, 50}}.

I can get material and height values simply doing:

{_, {_, Material}, {_, Height}} = Desk.

Now I’ll have two variables (Material and Height) with values ‘plastic’ and ’50’. Good 🙂

‘Tuples’

06/05/2009

Tuples allows to store things togheter, they must be declared using {} separating items with a ,
A simple example of tuple is {5, 10}.
Is a good habit to insert (as first item of the tuple) an Atom that represents what the tuple is, for example:

Person = {person,
{name, simone},
{age, 26}}.

Tuples are defined when you declare them and are destroyed when you don’t use them anymore (thanks garbage collector) so we haven’t to worry about memory allocation/deallocation.

You can use variables to create a tuple, for example:

Name = {name, simone}.
Age = {age, 26}.
Person = {person, Name, Age}.

1- In erlang every line of code must ends with a dot (.)

2- Every variable in erlang must begin with an uppercase letter

3- When you need to make a division (4/2)  you always get a float number (2.0), if you want an integer you have to use ‘div’ (4 div 2 -> 2)

4- Remainder of a division is ‘rem’ (5 rem 2 -> 1)

5- An Atom is ‘something’ that is non-numeric … it must begin with lowercase letter and can contain ‘_’ or ‘@’ in the name. They can be quoted with a single quotation mark. What is its value? The Atom itself (?)

For example the atom ‘cat’ values cat (for the moment honestly I haven’t much clear why it’s useful … let me see in the future :))

First post

06/05/2009

Hi all,

this is the first post (after introduction), this is what I already know about erlang.

1- Erlang is a programming language (obvious? :))

2- In Erlang a variable gets a value and mantains that value for the entire life of the program

3- You can easily install erlang on OSX using ports or compiling it by hand

4- You can launch erlang console typing $ erl

Hello

06/05/2009

Hi all, I’m trying to learn erlang and I’ll use this tumblr to remind myself what I learn while I’m studying it 🙂

If you have any suggestion, feedback, and so on I hope you will contact me by commenting a post.

See you!