[erlang-questions] questions about ERLANG!
Hynek Vychodil
vychodil.hynek@REDACTED
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:
add_up_list(List)->
LengthOfList=erlang:length(List)-1,
for_loop(0,LengthOfList,List).
for_loop(CurrentLocationInList,LengthOfList,List)->
if CurrentLocationInList==LengthOfList true->0;
false->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List).
Your 'if' satement is wrong. If statement in Erlang is very different. It
should be
if CurrentLocationInList==LengthOfList ->0;
true->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List)
end.
or use case statement which seems more like what you have written:
case CurrentLocationInList==LengthOfList of
true ->0;
false
->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List)
end.
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 <kiderlang@REDACTED>
> 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:
>
> -module(adder).
> -compile(export_all).
>
>
> add_up_list(List)->LengthOfList=erlang:length(List)-1,for_loop(0,LengthOfList,List).
> for_loop(CurrentLocationInList,LengthOfList,List)->if
> CurrentLocationInList==LengthOfList
> true->0;false->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List).
>
> 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
> wrong.
>
> 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
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
--
--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...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090328/cdebd5e1/attachment.htm>
More information about the erlang-questions
mailing list