Recursion on List

I’m new on Elixir and I want to know how to use properly recursion on Lists.

defmodule Make do       
	@my_head = [1]
	def create(1) do
		@my_head
	end
	
	def create(N) do
		@my_head = [head | create(N-1)]
	end
end

I want @my_head to be the list [1] and concatenate that list with other values without using module list functions. The problem is that I don’t know how to define a variable list inside my module Make to
concatenate it with other values.

That should be @my_head [1]

In Elixir, you can’t redefine @ variables (called Module Attributes) during runtime because they are constants defined at compile-time and, much like everything else in Elixir, are always immutable. Maybe try this?

	def create(n) do
		[n | create(n-1)]
	end
4 Likes

Yes, it worked thanks a lot