<HTML dir=ltr><HEAD><TITLE>Noob - Getting Started Infinte Loop?</TITLE>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2800.1561" name=GENERATOR></HEAD>
<BODY>
<DIV id=idOWAReplyText7420 dir=ltr>
<DIV dir=ltr><FONT face="Courier New" color=#000000 size=2>Hi, you have encapsulated TheRest in a list. Meaning that</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>you will recursively call listlen/1 with a list of one element</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>(the list of all remaining elements except the first one from the</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>first call to listlen/1). Then that call will end up at the second</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>clause, where the recursive call will be made on: listlen([[]])</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>which in its turn will end up on the second clause causing a</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>recursive call to listlen([[]]), and so on, until you run out</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>of stack.</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Best Regards,</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2>Lennart</FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" size=2></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" color=#000000 size=2>> listlen([]) -> 0;<BR>> listlen([First|TheRest]) -> 1 + listlen([TheRest]).<BR>                                          ^^^^^^^^^</FONT></DIV></DIV>
<DIV id=idSignature14434 dir=ltr>
<DIV><FONT face="Courier New" color=#000000 size=2>-------------------------------------------------------------</FONT></DIV>
<DIV><FONT face="Courier New" color=#000000 size=2>Lennart Ohman                   phone   : +46-8-587 623 27</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sjöland & Thyselius Telecom AB  cellular: +46-70-552 6735</FONT></DIV>
<DIV><FONT face="Courier New" size=2>Sehlstedtsgatan 6               fax     : +46-8-667 8230</FONT></DIV>
<DIV><FONT face="Courier New" size=2>SE-115 28 STOCKHOLM, SWEDEN     email   : <A href="mailto:lennart.ohman@st.se">lennart.ohman@st.se</A></FONT></DIV></DIV>
<DIV dir=ltr><BR>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>Från:</B> owner-erlang-questions@erlang.org genom fbg111<BR><B>Skickat:</B> on 2006-08-30 13:06<BR><B>Till:</B> erlang-questions@erlang.org<BR><B>Ämne:</B> Noob - Getting Started Infinte Loop?<BR></FONT><BR></DIV>
<DIV><BR>
<P><FONT size=2>Hi all, I'm an Erlang noob working through the getting started material at<BR>erlang.org, and I have a question about a problem that's occuring with my<BR>code.  If this isn't the appropriate place for this question, my apologies<BR>for wasting the bandwidth, please point me in the right direction.<BR>Otherwise, running the function listlen([1,2,...n]) crashes the Erlang<BR>emulator in WinXP.  However, I can't see the difference b/t my code and the<BR>example code:<BR><BR><A href="http://erlang.org/doc/doc-5.5/doc/getting_started/seq_prog.html#2.5">http://erlang.org/doc/doc-5.5/doc/getting_started/seq_prog.html#2.5</A> Example<BR>code:<BR><BR><BR>> The following example shows how we find the length of a list:<BR>><BR>> -module(tut4).<BR>> -export([list_length/1]).<BR>><BR>> list_length([]) -><BR>>     0;   <BR>> list_length([First | Rest]) -><BR>>     1 + list_length(Rest).<BR>><BR>> Compile (file tut4.erl) and test:<BR>> 29> c(tut4).<BR>> {ok,tut4}<BR>> 30> tut4:list_length([1,2,3,4,5,6,7]).<BR>> 7<BR>><BR><BR>My code:<BR><BR><BR>> -module(tut).<BR>> -export([double/1,fac/1,mult/2,convert/1,conv/2,listlen/1]).<BR>><BR>> double(X) -> 2 * X.<BR>> fac(0) -> 1;<BR>> fac(N) -> N * fac(N-1).<BR>> mult(X,Y) -> X*Y.<BR>> conv(M,toInch) -> M/2.54;<BR>> conv(N,toCentimeter) -> N*2.54.<BR>> convert({M,centimeters}) -> {M/2.54,inches};<BR>> convert({M,inches}) -> {M*2.54,centimeters}.<BR>> listlen([]) -> 0;<BR>> listlen([First|TheRest]) -> 1 + listlen([TheRest]).<BR>><BR><BR>And the result of running my code:<BR><BR><BR>> Erlang (BEAM) emulator version 5.5 [async-threads:0]<BR>><BR>> Eshell V5.5  (abort with ^G)<BR>> 1> c(tut).<BR>> ./tut.erl:13: Warning: variable 'First' is unused<BR>> {ok,tut}<BR>> 2> tut:listlen([]).<BR>> 0<BR>> 3> tut:listlen([1,2]).<BR>><BR>> Crash dump was written to: erl_crash.dump<BR>> eheap_alloc: Cannot allocate 583848200 bytes of memory (of type "heap").<BR>><BR>> Abnormal termination<BR>><BR><BR>Windows Task Manager confirms the process werl is trying to use 581MB of<BR>RAM.  Before I tried to run this, Task Manager showed 821MB RAM in use, out<BR>of 1GB.  Is this a problem with my code, or with my memory useage?  Any<BR>suggestions about what I'm doing wrong?  Thanks,<BR><BR>Byron<BR>--<BR>View this message in context: <A href="http://www.nabble.com/Noob---Getting-Started-Infinte-Loop--tf2189189.html#a6056733">http://www.nabble.com/Noob---Getting-Started-Infinte-Loop--tf2189189.html#a6056733</A><BR>Sent from the Erlang Questions forum at Nabble.com.<BR><BR></FONT></P></DIV></BODY></HTML>