[erlang-questions] create list quesion

Kyle Neal kyle@REDACTED
Tue Jan 27 16:07:42 CET 2015


I think we need a little more clarification...

On Tue, Jan 27, 2015 at 9:15 AM, Roelof Wobben <r.wobben@REDACTED> wrote:

> Hello,
>
> I try now to solve a challenge where I must make a list  from a range,
>
> So for example if I do create(2) the outcome have to be [ 1, 2]
>

Here you are saying create(N) should create a list from 1-N.


>
> So i can do
>
> create(0) ->
>   print array
>
> but as far as I know the list is then not in the scope so I cannot use it
> when I do
>
> create(2) ->
>   add the 2 to the array.
>

Here you are saying create(2) should ONLY add 2 to the list, when
previously you said create(2) should create a list from 1-2. Now, if you
are saying that create(N) should create a list from 1-N OR list:last()-N
(meaning it should be in range), then you would first have to check if N is
in the current list. If not, create the list, if so cancel

EX) (Using my assumption)

create(0) ->
         print array.

create(N) ->
          case lists:member(N, list) of
                     true ->
                              exit;
                      false ->
                              get last element of list (L),
                              append L-N to current list (L can also be
nothing, if this is the first call to this function in which case it should
append 1-N)
           end.



This should always work because the function is generating a list from A-B.
And you can using something like ets with ordered_set or something similar
to keep it in order.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150127/39bd0013/attachment.htm>


More information about the erlang-questions mailing list