[erlang-questions] create list quesion

Roelof Wobben r.wobben@REDACTED
Tue Jan 27 18:34:12 CET 2015


Roelof Wobben schreef op 27-1-2015 om 15:53:
> Hello,
>
> Oke,
>
> Something like this in pseudo code :
>
> create(0) ->
> print empty array
>
> create(x) ->
>   create_array(x, []) )
>
> create_array(0, array) ->
>  print array
>
> create_array(x, array) ->
>   add the first item to the array and run create_array again
>
> Roelof
>
>
>
>
>
> Loïc Hoguin schreef op 27-1-2015 om 15:17:
>> You can make a local function that takes more arguments and call it.
>>
>> On 01/27/2015 03:15 PM, Roelof Wobben 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]
>>>
>>> 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.
>>>
>>> How do I take care that the list is in the scope and still using only
>>> create(number)
>>>
>>> Can anyone give me a tip about it ?
>>>
>>> Roelof
>>>
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>

I have now this:

-module(create).

-export([create_list/2, create/1]).

create(0) ->
   [];

create(number) ->
   create_list(number, [] ).

create_list(0, [list]) ->
   [list];

create_list(number, [list]) ->
   create_list( number -1, [number | list] ).

but when i do create:create(1) I see this error :

** exception error: no function clause matching create:create(1) (create.erl, line 5) 


which is correct and wierd. Correct because create(0) is not the right one.
Wierd because it has to look at the second clause.

Roelof




More information about the erlang-questions mailing list