Noob - Getting Started Infinte Loop?

Vlad Dumitrescu vladdu55@REDACTED
Wed Aug 30 13:42:49 CEST 2006


Hi and welcome!

On 8/30/06, fbg111 <fbg111@REDACTED> wrote:
> However, I can't see the difference b/t my code and the
> example code:

> > list_length([]) ->  0;
> > list_length([First | Rest]) -> 1 + list_length(Rest).

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

The difference is that the last line in your code should read

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

i.e. TheRest and not [TheRest] as argument.

What happens is that listlen gets called with following arguments:
[1, 2, 3]
[[2,3]]
[[]]
[[]]
[[]]
[[]]
.....   so it's an endless recusrion that finally kills the VM

best regards,
Vlad



More information about the erlang-questions mailing list