[erlang-questions] why does the guard not working

Richard A. O'Keefe ok@REDACTED
Fri Feb 13 06:20:54 CET 2015

You were using display_numbers_loop/1 to print all integers between 1 and N.
Now you are using the same name for printing EVEN integers between 1 and N.
It would be very helpful to change the name.

In order to print even numbers in a range, you are generating ALL integers
in that range, and then throwing away the ones you do not want.

Fine, in that case

    display_numbers_loop(N, N) ->
    display_numbers_loop(N0, N) ->
        N1 = N0 + 1,
        case N1 rem 2
          of 0 -> io:format("Number: ~p~n", [N1])
           ; 1 -> skip
        display_numbers_loop(N1, N).

is the smallest change to your display_numbers_loop that will skip the
numbers you do not want.

But what's better still is not to generate the numbers you don't want
in the first place.

More information about the erlang-questions mailing list