[erlang-questions] why does the guard not working

Roelof Wobben r.wobben@REDACTED
Fri Feb 13 07:33:24 CET 2015


Richard A. O'Keefe schreef op 13-2-2015 om 6:20:
> 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) ->
>          ok;
>      display_numbers_loop(N0, N) ->
>          N1 = N0 + 1,
>          case N1 rem 2
>            of 0 -> io:format("Number: ~p~n", [N1])
>             ; 1 -> skip
>          end,
>          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.
>
>

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.

Roelof




More information about the erlang-questions mailing list