Update: This post has amazing traffic so it now has its own screencast
I have a strong gut feeling that Erlang is the next (current) big thing.
I started learning erlang with the very good and well written book by Joe Armstrong, Programming Erlang: Software for a Concurrent world . I am trying to keep a summary of what I am learning and so I started writing it as a little note. Doing so I noticed that I am able to squeeze a lot of pages into few lines and thought that someone else apart from me MIGHT also understand what I meant as I wrote it as if I was explaining Erlang to another Rubist. I hope I am right.
* I have tried to remain superficial and not to get into details in order to keep things as simple as possible to start with.
The list is here, I might also produce a small screen cast and part #2 for this in the coming future. Enjoy!
- end every sentence with a dot.
- you can write numbers with base declaration: BASE#NUMBER.
example: 15 is like 10#15 or 16#f - erlang has no variables, only constants.
- erlang's constants are being called variables.
- constants start with a capital letter:
C = 5.
CapitalLetter = 5. - divide floats with "/"
5 / 3 => 1.6667
5.25 / 3 => 1.75 - divide integer with "div"
5 div 3 => 1 - modulo = reminder = "rem"
5 rem 3 => 2 - "string" is written with double-quotes
- atom = ruby :symbol is written as is (starts only with lowercase letter)
johnny_walker - 'something' acts different then "something" or symbol. still need to figure it out.
- kind of array = tuple
defined with {}
used broadly in erlang, also to replace complex data structures you would create a class for in ruby.
NiceTuple = { 10, 45 }.
NicerTuple = { 10, joe, 45, david }. - {X,Y,Z} = {1,2,3}.
X => 1
Y => 2
Z => 3
like multiple assignment in ruby:
x,y,z = 1,2,3
it works just the same in these cases. - {something,X} = {something,5}.
x => 5
BUT
{something,X} = {something_else,5}.
wont set X (!)
"=" sets if left tuple structure fits right tuple structures in length and position of atoms/strings.
{"gran torino",X} = {"gran torino",5}.
X => 5 - err: {something,X,X,X} = {something,1,2,3}.
- {_,_,_,koko,5,X,_,_} = {i,dont,care,koko,5,123,whats,here}.
X => 123 - arrays on steroids = lists
defined with []
[1,2,3,[4,5]]
[i,ate,{a,tasty,cake}] - SmallArray = [3,4].
BigArray = [1,2|SmallArray].
BigArray => [1,2,3,4]. - [First_element|All_the_rest] = Array.
[First_element|All_the_rest] = [1,2,3,4,5].
First_element => 1
All_the_rest => [2,3,4,5] - [First,Second,Third|All_the_rest] = Array.
[First,Second,Third|All_the_rest] = [1,2,3,4,5,6].
First => 1
Second => 2
Third => 3
All_the_rest = [4,5,6]. - {X,Y,Z} = {1,koko,"loko"}.
X => 1
Y => koko
Z => "loko" - modules: Erlang's basic unit structure, comes in a file
filename.erl
erlang compiles it and then its called
filename.beam - the structure of a .erl file:
-module(file_name).
-export([function_name/1]).
function_name(X) -> 0.
---------------------------
this amazing module should then be called from the erl console with
c(file_name)
and then
file_name:function_name(1) => 0
file_name:function_name(koko) => 0
file_name:function_name(whatever) => 0 - a more interesting function:
-module(file_name).
-export([function_name/1]).
function_name(true) -> 'funny';
function_name(false) -> 'not funny'.
-----------------------------
this function returns different values for "true" and for "false" - one nicer example:
-module(file_name).
-export([fib/1]).
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).

2 comments:
Awesome! Waiting for part II ;-)
Single-quoted "strings" are actually atoms. A common one you'll find in Erlang is 'EXIT', which tags the exit codes of processes.
Post a Comment