Friday, February 15, 2013

Some Commented Erlang

Was pondering the whole von Neumann bottleneck cf. Quantum computing  .....

 (LOL not really)

Was reading this python doc on function programming  a  (longish) while back ...

Decided to go snorkling into concurrent programming  etal. ...
So  l o n g   s t o r y  short :
The intention is to learn some sound(er) principles from a number of places and apply them to my work generically where possible, after all why not, it's all good..

FWIW I'll drop some stuff more or less randomly here and make some arrogant pontifications later :)

The  primary languages of exploration are Erlang and Python so far... possibly C soonish.
(thats more than I need or possibly  even deserve :)

Turns out I have accidentally  learnt (a lot) from python anyhow [gasp!] , and even applied some rudiments  to my standard bash approach. [ bigger gasp :>]

IMHO Erlang seems to have an _undeserved_ "hairy" reputation. 

Here is a commented (day 2 level)  Erlang module.

I extended it slightly and added comments that helped me.
Perhaps they may help somebody else.

Mostly from an  excellent Erlang course ,.

-export([factorial/1, area/1]).

%% from
%% (not original) -author('pete@ruthie.tux').

%%%% area :: calculate square circle or triangle areas 
%%%% match atom  to evaluate Variables

area({square, Side}) -> % this tuple made of: {atom, Variable} 
    Side * Side;
area({rectangle, Length, Width}) -> % this made of {atom, Variable, Variable}
    Length * Width;
area({circle, Radius}) -> % well almost PI is NAN (here we only do radii)
    3.14 * Radius * Radius;
area({triangle, A, B, C}) ->
    S = (A + B + C)/2,
%% that must be inside -> out eval ... like "normal"
area(Other) ->
    {woops_invalid_tuple_content, Other}. % any un matched atom spit error

%%%% single parameter only exported at module head ?  
%%%% Its OK we require {a, {tuple, as}} input parameter(s)

%% return the factorial of the single parameter
%%  disallow div by 0 ! It really hurts. (fire extinguisher required)
%%  a guarded function  in two clauses
factorial(N) when N > 0 ->
    N * factorial(N - 1);
factorial(0) -> 1.

The code would live in a module (file) named mathStuff.erl and be compiled from
 the erl shell or whatever method (emacs etc) you like.


~/ erl

Eshell V5.9.1  (abort with ^G)
1> c(mathStuff.erl).

(on a perfect day :)
It will compile the x.erl file > x.beam file.
Everything is named the same.
There are header (.hrl z :) files as well and includes may have records, even "macros"  etc ... just haven't got to them yet.

From an erl shell (and the same directory for simplicity) you can call this like so:

61> mathStuff:area({triangle, 3, 4, 5}).

(the 61st thing I had done in the shell)
So the area of the triangle is 6 whatevers ... great :)

We asked the module mathStuff for its exported function area() to "process" (->)
the tuple  {triangle, 3, 4, 5}


62> mathStuff:factorial(3).

63> mathStuff:factorial(6).

Random Observations so far.

 module:foo() is way more readable than most anything else I have seen.
(yeah I know you can intit to whatever candy you like in most languages.)

Erlangs reads reasonably easily even after just an hour or two.. once you see where "output" is coming from.  There need be no print() though there is a thing similar to "printf".

One of the ideas is to have no side-effects
(and bounce / break on invalid input.)

Actually a core idea is to NOT muck about (as much) with allocated memory.
This language cleans up ASAP as well.

Lots to grok if you haven't seen Erlang before but its actually elegant and simple.
If you are used to python you wont have issue with the function structures think of them as
if elseifs or case switch statements ... though you wont for long. :)

A function is simply a collection of clauses to match and ends with a full stop.

Think of the fullstop  as "complete ! Now Grunge".

Yep there are Objects. (well not formal classes (yet) but I guess you could )
(go use python or java or C++ or Perl)

Variables Must Have At Least The First Letter Capitalised.
Variables once bound are not reset (per instance) if you try it crashes the function.
(and quite loudly too :)

So there are no i++ (or "normal" loops) for example., and no need.

Emacs has very good support for the language. And you can run a shell in a second window.
(and learn elisp someday)

The  Erlang Docs site has excellent documentation about the hows and whys.
If nothing else browse the site and contemplate:
  concurrency parallelism 
  "threads" Processes 
  imperative VS declarative . 
  functional programming [1]  
(or not :)

And yep I did div/0 , not a good look.
Mind you I've seen worse runaways.

apt-cache search erlang
(or whatever your nix does)
[1] see also: the extended python docs (comes with a full install) about this.
Its what got me wondering about it all.

No comments:

Post a Comment