[erlang-questions] why does the guard not working

Richard A. O'Keefe ok@REDACTED
Fri Feb 13 07:51:50 CET 2015


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.





More information about the erlang-questions mailing list