[erlang-questions] why does the guard not working

Roelof Wobben r.wobben@REDACTED
Thu Feb 12 15:51:02 CET 2015


Thanks ,

Stupid mistake I had to use rem.

Now i have this :

-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 rem 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 rem 2 /= 0 ->
  display_numbers_loop(Current +1, Number ).

but now I see only the odd numbers which I find wierd because  I say 
when Current rem 2 ==0 and that true for even numbers.
So then the io can be found.

Roelof



Bengt Kleberg schreef op 12-2-2015 om 15:38:
> 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
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>




More information about the erlang-questions mailing list