[erlang-questions] why does the guard not working

Roelof Wobben r.wobben@REDACTED
Fri Feb 13 08:01:08 CET 2015


Richard A. O'Keefe schreef op 13-2-2015 om 7:51:
> On 13/02/2015, at 7:33 pm, Roelof Wobben <r.wobben@REDACTED> wrote:
>> Clear explanation.
>>
>> I have to think how I can not generate the numbers I do not want.
>> I think I can use the same idea that you have used to display the even numbers.
> In C, to print the even integers between 1 and N:
>
>      for (int I = 2; I <= N; I += 2) {
>          printf("Number: %d\n", I);
>      }
>
> Of course, if you know how to iterate over the elements of a list,
> you could start by making a list of the numbers you want, and
> then iterate over that.  For example, you could write
>
> print_even_integers(N) when is_integer(N), N >= 0 ->
>      lists:foreach(
>          fun (X) -> io:format("Number: ~p~n", [X]) end,
>          lists:seq(2, N, 2)).
>
> because (recursive) functions "generate an additive sequence of
> integers" and "do this to every element of that list" have already
> been written.  It has been argued that functional programming is
> not so much about functions as about writing program pieces you
> can just plug together.
>
>

Thanks,

The problem is here you use the lists functions and I think Im now 
allowed to use them.

Roelof




More information about the erlang-questions mailing list