Help - a function that prints numbers in ascending order, until the provided parameter

Good Day All

How do I get to create or change this function to print out the Numbers in ascending order
if the provided variable is 4 the output should be
0
1
2
3
4
currently, I have created this one it prints in decending order, if input 4 output is :
4
3
2
1
0
here is the function:

def print(-1), do: :done
def print(n) do
puts("#{n} is #{[n]}")
print (n - 1)
end

Welcome to the forum.

iex> fun = fn n -> 0..n |> Enum.each(fn i -> IO.puts(i) end) end
#Function<44.79398840/1 in :erl_eval.expr/5>
iex> fun.(4)
0
1
2
3
4
:ok
2 Likes

Hello and welcome,

As mentionned using range is the easiest way.

0…n or n…0 in descending order.

But if You want to make it recursive, You need to pass some options, and take care of exit.

def print(-1, order: :desc), do: :done
def print(n, order: :asc, initial_value: n), do: :done
etc.

Please add ``` around your code to make it more readable :slight_smile:

1 Like

Thanks man! :slight_smile:

Thanks for your suggestion, I actually wanted it to be recursive. :slight_smile: