[erlang-questions] output [[[[]|2]|1]|0] instead of [2.1.0]

Richard A. O'Keefe ok@REDACTED
Wed Feb 11 03:56:31 CET 2015


On 11/02/2015, at 3:19 am, Roelof Wobben <r.wobben@REDACTED> wrote:

> Hello,
> 
> I have to make a reversed list to a number so reverse_list(2) will output [2.1]

Why should an integer 2 yield a floating point 2.1?
> 
> I have this code :
> 
> -module(list_creation).

You are still trying to think in imperative, possibly object-oriented terms.
There is no creation here.
> 
> -export([create/1, create_reverse/1]).

These are bad names.  Create (what does that mean?) WHAT?

I think you want something like

    ascending_counts(2) -> [0,1,2]
    descending_counts(2) -> [2,1,0]

> 
> create(Number) when Number >= 0 ->
>  create_acc(Number,[]).

I find the suffix "_acc" totally unhelpful.
What is an acc?  Or what is accing?  What does it mean
to create an acc?

descending_counts(N) when is_integer(N), N >= 0 ->
    [N | descending_counts(N - 1)];
descending_counts(N) when is_integer(N), N < 0 ->
    [].

ascending_counts(N) when is_integer(N), N >= 0 ->
    ascending_counts_loop(N, []).

ascending_counts_loop(N, Counts) when N >= 0 ->
    ascending_counts_loop(N - 1, [N|Counts]);
ascending_counts_loop(N, Counts) when N < 0 ->
    Counts.

> 
> but when I do create_reverse(2) I see this output [[[[]|2]|1]|0] instead of [2,1,0]
> 
> Does the ++ always produce this when adding a item to a list ?

++ DOES NOT ADD AN ITEM TO A LIST.

++ *concatenates* TWO lists.

Again, what you should do is TRY IT.

% erl
1> Acc = [].
[]
2> [Acc] ++ 1.
[[]|1].

Presumably what you mean is
3> Acc ++ [1].
[1]






More information about the erlang-questions mailing list