I am working on Elixir and having an issue with if/case statement. In a recursive function, I want to check if a value has been previously initialized or not, if not I shall set the value, but I am always getting CompileError:
def func_a() do
if {v is undefined} do
v = 1
end
...
func_a()
end
I want v to be set to 1 during the first iteration, during the later iterations, I don't need to run that if statement anymore. I am wondering how to check if v is undefined without causing any error (otherwise it won't compile)? Thank you!
To be specific: I need a counter to be added in a function, and I define the counter like this:
def get_counter(i \\1) do
fn -> {i, get_counter(i + 1)} end
end
In my major function:
def func_a(params) do
c = get_counter()
receive do
something
end
after
@timeout ->
{v,c} = c.() # increment the counter v
func_a(params)
end
end
I am still confused about how could I bypass c = get_counter() during the later iteration of recursion?