Noob - Getting Started Infinte Loop?

ke han <>
Wed Aug 30 15:02:19 CEST 2006


Byron,

A few notes in regards to this line of code:

>> listlen([First|TheRest]) -> 1 + listlen([TheRest]).

1 - your error appears to be that you are laking the tail of the  
list, bound as TheRest, and putting it back inside another list by  
your call to listlen([TheRest]).  This is a common type of bug, no  
shame here, I'm sure I've done it before.  TheRest is already a list,  
your tail recursive call is wrapping it inside another list with each  
call.  So, yes you will run out of memory ;-)

2 - I would always put a space between my variable names and the |  
symbol.  Your code:  [First|TheRest] needs to be more readable.   I  
always write code that has good spacing / formatting both for  
readability and to ensure I never hit any potential edge/corner cases  
of the compile toolchain.   This is a general programming rule.  I  
know of no reasons why erlang would read your code incorrectly.

3 - Also, for efficiency sake, you should name the variable "First"  
as "_First" to indicate you do not want the value bound.

have fun, ke han


On Aug 30, 2006, at 7:06 PM, fbg111 wrote:

>
> Hi all, I'm an Erlang noob working through the getting started  
> material at
> erlang.org, and I have a question about a problem that's occuring  
> with my
> code.  If this isn't the appropriate place for this question, my  
> apologies
> for wasting the bandwidth, please point me in the right direction.
> Otherwise, running the function listlen([1,2,...n]) crashes the Erlang
> emulator in WinXP.  However, I can't see the difference b/t my code  
> and the
> example code:
>
> http://erlang.org/doc/doc-5.5/doc/getting_started/seq_prog.html#2.5  
> Example
> code:
>
>
>> The following example shows how we find the length of a list:
>>
>> -module(tut4).
>> -export([list_length/1]).
>>
>> list_length([]) ->
>>     0;
>> list_length([First | Rest]) ->
>>     1 + list_length(Rest).
>>
>> Compile (file tut4.erl) and test:
>> 29> c(tut4).
>> {ok,tut4}
>> 30> tut4:list_length([1,2,3,4,5,6,7]).
>> 7
>>
>
> My code:
>
>
>> -module(tut).
>> -export([double/1,fac/1,mult/2,convert/1,conv/2,listlen/1]).
>>
>> double(X) -> 2 * X.
>> fac(0) -> 1;
>> fac(N) -> N * fac(N-1).
>> mult(X,Y) -> X*Y.
>> conv(M,toInch) -> M/2.54;
>> conv(N,toCentimeter) -> N*2.54.
>> convert({M,centimeters}) -> {M/2.54,inches};
>> convert({M,inches}) -> {M*2.54,centimeters}.
>> listlen([]) -> 0;
>> listlen([First|TheRest]) -> 1 + listlen([TheRest]).
>>
>
> And the result of running my code:
>
>
>> Erlang (BEAM) emulator version 5.5 [async-threads:0]
>>
>> Eshell V5.5  (abort with ^G)
>> 1> c(tut).
>> ./tut.erl:13: Warning: variable 'First' is unused
>> {ok,tut}
>> 2> tut:listlen([]).
>> 0
>> 3> tut:listlen([1,2]).
>>
>> Crash dump was written to: erl_crash.dump
>> eheap_alloc: Cannot allocate 583848200 bytes of memory (of type  
>> "heap").
>>
>> Abnormal termination
>>
>
> Windows Task Manager confirms the process werl is trying to use  
> 581MB of
> RAM.  Before I tried to run this, Task Manager showed 821MB RAM in  
> use, out
> of 1GB.  Is this a problem with my code, or with my memory useage?   
> Any
> suggestions about what I'm doing wrong?  Thanks,
>
> Byron
> -- 
> View this message in context: http://www.nabble.com/Noob---Getting- 
> Started-Infinte-Loop--tf2189189.html#a6056733
> Sent from the Erlang Questions forum at Nabble.com.
>




More information about the erlang-questions mailing list