viernes, 25 de julio de 2014

My first post on Erlang

So...a couple of days ago I decided (for the second time) to learn Erlang...but this time...I'm taking it serious -;)

But...what the hell is Erlang? Put it simple...it's a multi-paradigm, concurrent and functional programming language...simple, huh?

Erlang is a really nice and beautiful language...but...after this first couple of days...my brain is starting to hurt...I will let you know later why...

Anyway...I'm reading Learn You Some Erlang for Great Good...that can be read online here or bought here. I bought it -;)

So...you can expect a nice review as soon as I finish reading it...

As I usually do with new programming languages...I wanted to port something I have done before into Erlang...because for me...that's the best way to learn...

I took the code for an LED Output that I have built for both Python and Julia and ported to Erlang...read the blog here.

Here's the Erlang code...

led.erl
-module(led).
-export([showLED/0,showLED/1]).

printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3)
 when Counter > Long ->
  [io:format("~s",[X]) || X <- Line1], io:format("~n"),
  [io:format("~s",[X]) || X <- Line2], io:format("~n"),
  [io:format("~s",[X]) || X <- Line3], io:format("~n");
  
printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3)
 when Counter =< Long ->
 printLines(Counter+1,Long,Lines,Digits,
      Line1 ++ [element(1,element(1,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))],
      Line2 ++ [element(1,element(2,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))],
      Line3 ++ [element(1,element(3,X)) || {X,Y}<-Lines, Y == element(Counter,list_to_tuple(Digits))]).

showLED() ->
 io:format("You must enter a number!~n").
            
showLED(Number) ->
 Lines = [{{{" _  ",1},{"| | ",2},{"|_| ",3}},0},{{{"  ",1},{"| ",2},{"| ",3}},1},
   {{{" _  ",1},{" _| ",2},{"|_  ",3}},2},{{{"_  ",1},{"_| ",2},{"_| ",3}},3},
   {{{"    ",1},{"|_| ",2},{"  | ",3}},4},{{{" _  ",1},{"|_  ",2},{" _| ",3}},5},
   {{{" _  ",1},{"|_  ",2},{"|_| ",3}},6},{{{"_   ",1},{" |  ",2},{" |  ",3}},7},
   {{{" _  ",1},{"|_| ",2},{"|_| ",3}},8},{{{" _  ",1},{"|_| ",2},{" _| ",3}},9}],
 Line1 = "", Line2 = "", Line3 = "", Counter = 1,
 Numbers = integer_to_list(Number),
 Long = length(Numbers),
 Digits = lists:map(fun(X) -> X - 48 end, Numbers),
 printLines(Counter,Long,Lines,Digits,Line1,Line2,Line3).

and here's the output...


So...I'm going to tell you why my brain hurts...and I hope this doesn't make you stay away from Erlang...au contraire...you really should give Erlang a change -;)

First...Variables are invariable...which means...once A = 1 then A is going to be 1 forever and ever...no matter how many times you do A = 2...A is going to keep being 1...and of course you're going to get an error if you do A = 2...because A is 1...got it!

Second...No loops...no For's...no While's...no Do...While's...nothing...just recursion...functions that call themselves...multiple times until something makes them stop...

Third...String...are not strings...yep...no strings in Erlang...everything is integer...so in the end "A" is 65...

Forth...Object Orientation...doesn't even try it...no good...this is functional programming...forget about classes, methods, attributes, friends, templates...and so on...

Ok...why would you want to learn Erlang after all this non-sense? Easy...because Erlang changes your mindset completely...forgot everything you have ever learned...you need to think and do things in a completely different way...but believe me...after a couple of days with Erlang...you're going to be a better developer...that's guaranteed...

Greetings,

Blag.
Development Culture.

No hay comentarios: