SV: Noob - Getting Started Infinte Loop?

Lennart Öhman <>
Wed Aug 30 13:22:55 CEST 2006


Hi, you have encapsulated TheRest in a list. Meaning that
you will recursively call listlen/1 with a list of one element
(the list of all remaining elements except the first one from the
first call to listlen/1). Then that call will end up at the second
clause, where the recursive call will be made on: listlen([[]])
which in its turn will end up on the second clause causing a
recursive call to listlen([[]]), and so on, until you run out
of stack.
 
Best Regards,
Lennart
 
 
> listlen([]) -> 0;
> listlen([First|TheRest]) -> 1 + listlen([TheRest]).
                                          ^^^^^^^^^
-------------------------------------------------------------
Lennart Ohman                   phone   : +46-8-587 623 27
Sjöland & Thyselius Telecom AB  cellular: +46-70-552 6735
Sehlstedtsgatan 6               fax     : +46-8-667 8230
SE-115 28 STOCKHOLM, SWEDEN     email   : 

________________________________

Från:  genom fbg111
Skickat: on 2006-08-30 13:06
Till: 
Ämne: Noob - Getting Started Infinte Loop?




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.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060830/93d7d82b/attachment.html>


More information about the erlang-questions mailing list