[erlang-questions] why does the guard not working

Roelof Wobben r.wobben@REDACTED
Thu Feb 12 15:33:58 CET 2015


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




More information about the erlang-questions mailing list