If you want sum up list you can do it simply in this way:<br><br>add_up_list([]) -> 0;<br>add_up_list([H|T]) -> H + add_up_list(T).<br><br>or in tail call manner<br><br>add_up_list(L) -> add_up_list(L, 0).<br><br>
add_up_list([], X) -> X;<br>add_up_list([H|T], X) -> add_up_list(T, H+X).<br><br>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:<br>
<br>add_up_list(List)-><br> LengthOfList=erlang:length(List)-1,<br> for_loop(0,LengthOfList,List).<br>for_loop(CurrentLocationInList,LengthOfList,List)-><br> if CurrentLocationInList==LengthOfList true->0;<br>
false->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List).<br><br>Your 'if' satement is wrong. If statement in Erlang is very different. It should be<br><br> if CurrentLocationInList==LengthOfList ->0;<br>
true->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List)<br> end.<br><br>or use case statement which seems more like what you have written:<br>
<br> case CurrentLocationInList==LengthOfList of<br> true ->0;<br>
false ->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List)<br> end.<br>
<br>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.<br>
<br><div class="gmail_quote">2009/3/28 Kid Erlang <span dir="ltr"><<a href="mailto:kiderlang@gmail.com">kiderlang@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
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.<br>
<br>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:<br><br>-module(adder).<br>
-compile(export_all).<br><br>add_up_list(List)->LengthOfList=erlang:length(List)-1,for_loop(0,LengthOfList,List).<br>for_loop(CurrentLocationInList,LengthOfList,List)->if CurrentLocationInList==LengthOfList true->0;false->lists:nth(CurrentLocationInList+1,List)+for_loop(CurrentLocationInList+1,LengthOfList,List).<br>
<br>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:<br><br>~$ erlc adder.erl<br>./adder.erl:5: syntax error before: true<br>./adder.erl:4: function for_loop/3 undefined<br>
<br>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.<br><br>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!<br>
<font color="#888888">
<br>- Kid Erlang<br>
</font><br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br>
<br>Analyze your data in minutes. Share your insights instantly. Thrill your boss. Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>