[erlang-questions] is this a well written function
Roelof Wobben
r.wobben@REDACTED
Thu Feb 12 08:03:22 CET 2015
Richard A. O'Keefe schreef op 12-2-2015 om 3:25:
> On 11/02/2015, at 8:45 pm, Roelof Wobben <r.wobben@REDACTED> wrote:
>> -export([display_numbers/1]).
>>
>> %% displays(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.
> The comment disagrees with the code.
> The comment says the function call is 'displays(_)','
> but the code says it is 'display_numbers(_)'.
>
> The comment disagrees with the code.
> The comment says "when N is a NON-NEGATIVE INTEGER",
> but the code insists on a STRICTLY POSITIVE number of
> any kind. So the comment would accept 0 and reject 1.2,
> while the code rejects 0 and accepts 1.2.
>
>> display_numbers(Number) when Number > 0 ->
>> display_numbers_loop(Number, 1).
> You would do better to stick with the comment,
> and to count UP from 0, so that the control argument
> actually means something: it counts how many numbers
> already printed. Also, control arguments are
> conventionally written first.
>
> display_numbers(N) when is_integer(N), N >= 0 -> % 1; A,B
> display_numbers_loop(0, N). % 2; C
>
> % In display_numbers_loop(N0, N), N is how many numbers
> % we want to print altogether, and N0 is how many
> % numbers we have already printed.
>
> display_numbers_loop(N, N) -> % 3; C
> ok; % 4; F
> display_numbers_loop(N0, N) -> % 5;
> N1 = N0 + 1, % 6; C,D
> io:format("Number: ~p~n", [N1]), % 7; D
> display_numbers_loop(N1, N). % 8;
>
>
Thanks for all the remarks. I changed it all but one remark about your
code ;
When I do N1 = N0 + 1 then I get a error because N1 is imutable and
cannot be changed.
So the new code will be :
-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
%% the user has given 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) ->
io:format("Number:~p~n",[Current + 1]),
display_numbers_loop(Current +1, Number ).
Roelof
More information about the erlang-questions
mailing list