[erlang-questions] questions about ERLANG!
Sat Mar 28 11:40:26 CET 2009
If you want sum up list you can do it simply in this way:
add_up_list() -> 0;
add_up_list([H|T]) -> H + add_up_list(T).
or in tail call manner
add_up_list(L) -> add_up_list(L, 0).
add_up_list(, X) -> X;
add_up_list([H|T], X) -> add_up_list(T, H+X).
What is wrong with your program? It is prelish ;-) I'm used to write
thousands of SLOC in Perl professionally, but in Erlang you should write
programs in differnet way. Firs think: make short statements and functions.
Wrap to lines:
if CurrentLocationInList==LengthOfList true->0;
Your 'if' satement is wrong. If statement in Erlang is very different. It
if CurrentLocationInList==LengthOfList ->0;
or use case statement which seems more like what you have written:
case CurrentLocationInList==LengthOfList of
Your program is not efficient. My first version is O(N) in time and O(N) in
memory. Second one (tail call) is O(N) in time and O(1) in memory. Your
vesrion is O(N^2) in time and O(N) in memory. For perl developer is
confusing that Erlang doesn't have array! List is List e.g. conected list.
Each call lists:nth case inner loop. Decoupling lists by head cut ([_|_]) is
far better. In Erlang you should use pattern matching a lot and than your
programs become elegant.
2009/3/28 Kid Erlang <>
> hi everybody. I have just discovered Erlang! it is very exciting. I have
> been making scripts with perl that call each other so I have hundreds of
> scripts running at once. my friend told me Erlang has processes so I
> thought I could replace my scripts with Erlang! I had bought the book
> programming Erlang. very good book and fun to read, but I still have
> questions about Erlang! it is my first functinal language and so far that
> has been pretty confusing! I am still reading the book too.
> recursive blew my mind! I have tried forever on this code and I finally
> have it working. there are no for loops in erlang so I had to write my own
> into recursive. here is the program what I came up with:
> the goal of this program is to take all the numbers of the list and add
> them together. but it will not compile! here is the error:
> ~$ erlc adder.erl
> ./adder.erl:5: syntax error before: true
> ./adder.erl:4: function for_loop/3 undefined
> where is my program wrong? I have written programs before this and they
> compiled. but this one really stumps me. I cannot figure out what is
> I have not got to the paralell part of Erlang yet but I hear it lets you
> automagically scale into the cloud! that is my goal with Erlang!
> - Kid Erlang
> erlang-questions mailing list
--Hynek (Pichi) Vychodil
Analyze your data in minutes. Share your insights instantly. Thrill your
boss. Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions