[erlang-questions] Please review this basic Erlang doc?

Rodrigo Ahumada M. rodahummont@REDACTED
Fri Feb 2 04:39:29 CET 2007

El jue, 01-02-2007 a las 17:17 -0500, Robert Baruch escribió:
> Hi all,
> In learning Erlang as an expert procedural developer, but novice  
> functional developer, I had to organize my thoughts about Erlang and  
> how to think in functional terms. So, I wrote a document, and decided  
> that maybe it's useful to other Erlang beginners who are also  
> procedural experts.
> With that in mind, does anyone want to take a look at this document  
> and send in comments? It's not finished, so I'd welcome any suggestions.
> The document doesn't aim to be an Erlang tutorial, but rather to  
> guide the developer in thinking functionally. So once I'm sure a  
> functional concept has been explained, I do a lot of referring the  
> reader to the reference manuals.
> Anyway, here it is: http://chuffyrodents.org/erlang.pdf


i'm just a student that recently started to try coding with Erlang, and
one thing i had to put effort was when dealing with loops and the
inmutability of variables, the first thing that came to mind was:

 "what wicked recursive function i have to write now to process this

it took me some time to get used to mix map and fold, etc. so i think it
could be good if you can put more examples of that kind, with some
common cases.

i find it's more difficult when you have to compare each elements of a
list with the others, and when you have to return the indexes of
elements from a list that meet some condition.

an example, a simple problem:

i got a list of vectors and i want a function that returns the indexes
of the two closest vectors, and their distance.

a first approach is to recreate a list with all the combinations of two
vector, their indexes and distance, and then choose the closest, but it
could grow too much with big lists.

so i find difficult to write a recursive function that can go comparing
each vector, tracking the vector index and discarding previous data for
the sake of efficiency.

the best thing i could get was this:

-export([closer/1, vector_dist/2, test/0]).

% vector_dist(): returns de distance between 2 vectors (vector are 
% simply lists of numbers).
% closer(List) -> returns the indexes of the closer vectors from List 
% and their distance.
%   the format is: {{Index1, Index2}, Distance}
% test(): just a test to probe the function.

vector_dist(V1, V2) ->
  math:sqrt(lists:sum(lists:zipwith(fun(A, B) -> 
					C = A - B,
					C * C
				    end, V1, V2))).

closer([]) -> {};

closer([Only]) -> Only;

closer([First, Last]) -> {{1, 2}, vector_dist(First, Last)};

closer(List) ->
  closer1(List, 1, 2, {{-1, -1}, 10000000}).

min_result(NewResult={_, NewDist}, OldResult={_, OldDist}) ->
    NewDist < OldDist ->
    true ->

closer1([_Last], _, _, Result) -> Result;

closer1([First, Second | Rest], IFirst, ISecond, OldResult) ->
  io:format("closer 1: ~p ~p~n", [IFirst, ISecond]),	
  NewResult = min_result({{IFirst, ISecond}, vector_dist(First,
Second)}, OldResult),
  closer1([First | Rest], IFirst, ISecond + 1, closer2([Second | Rest],
ISecond, ISecond + 1, NewResult)).

closer2([_Last], _, _, Result) -> Result;

closer2([First, Second | Rest], IFirst, ISecond, OldResult) ->
  io:format("closer 2: ~p ~p~n", [IFirst, ISecond]),
  NewResult = min_result({{IFirst, ISecond}, vector_dist(First,
Second)}, OldResult),
  closer2([First | Rest], IFirst, ISecond + 1, NewResult).

test() ->
  M = [[210], [710], [64], [780], [42], [130], [400], [915], [90]],

it's difficult to understand, and it works search from the last to first

i don't know if there's a better way to achieve this.

anyway, excuse my english.


Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the erlang-questions mailing list