Elixir is a language with immutable data. You cannot update a value, you can only create a new one. So it needs to be new_list = List.insert_at(list, -1, num)
Lexical scoping allows you to reassign an existing variable a new value, but that variable will be scoped to the current scope and won’t probagate to outer scopes. A fordo/end block is a scope boundary, so variables assigned within won’t be accessible from the outside (even if they shadow a variable set on the outside as well).
This means you’d need to restructure your approach, so it can actually return your aggregated result.
The third thing:
3. for indicates a comprehension in Elixir, not a “for loop”. They’re much more powerful, and, in fact, your solution is not far from a one line for-comprehension solution