[erlang-questions] why does the guard not working

Erik Søe Sørensen eriksoe@REDACTED
Thu Feb 12 17:47:34 CET 2015


Not so odd - look at the next line, at what you're telling it to print
(it's not Current).
Den 12/02/2015 15.51 skrev "Roelof Wobben" <r.wobben@REDACTED>:

> 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
>>
>>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150212/981539bc/attachment.htm>


More information about the erlang-questions mailing list