[erlang-questions] why does the guard not working

Bengt Kleberg bengt.kleberg@REDACTED
Thu Feb 12 15:38:27 CET 2015


Have you tested
Current / 2 == 0
with some values for Current?
It is only true when Current is 0.

The erlang shell is your friend for simple tests like this.


bengt

On 02/12/2015 03:33 PM, Roelof Wobben wrote:
> Hello,
>
> I have this code :
>
> -module(side_effects).
>
> -export([display_numbers/1]).
>
> %%  display_numbers(N) displays the numbers from  1,2,...+N when N is 
> a non-negative integer.
> %%  It is not defined for other arguments.
> %%  When N is a non-negative number, a helper function is called so it 
> prints out
> %%  the numbers in the right order. This is a exercise from the Erlang 
> Programming book
> %%  where I have to practice side-effects.
> display_numbers(Number) when is_integer(Number), Number > 0 ->
>   display_numbers_loop(0, Number ).
>
> %%  When the control argument)(second argument) is equal to the number
> %%  the user has given, the end is reached and the last number is printed
> display_numbers_loop(Number, Number) ->
>   ok;
>
> %%  When the contro argument(second argument) is not equal to the number
> %%  and the Current number is even,
> %%  The control argument is increased by one and the
> %%  display_numbers_loop function is called again with the new arguments.
> display_numbers_loop(Current, Number) when Current / 2 == 0 ->
>  io:format("Number:~p~n",[Current + 1]),
>  display_numbers_loop(Current +1, Number );
>
> %%  When the contro argument(second argument) is not equal to the number
> %%  and the Current number is not even,
> %%  The control argument is increased by one and the
> %%  display_numbers_loop function is called again with the new arguments.
> display_numbers_loop(Current, Number) when Current / 2 /= 0 ->
>  io:format("Number:~p~n",[Current + 1]),
>  display_numbers_loop(Current +1, Number ).
>
> but when I do side_effects(10) I see all the numbers so I wonder why 
> the when is not working ?
>
> Roelof
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list