mazzie

mazzie

Appending elements to a list

I have a list. I am trying to choose specific combinations from that list and making a list of list of those combinations.
My code runs something like this:

n = 6
list = [1,2,3,4,5,6]
n_list =
for i ← n..1 do
temp_list = List.delete_at(list,i-1)
[temp_list | n_list]
end

So basically i am removing one element at a time adding the resultant list to my list of list. temp_list is getting created correctly in each iteration. However that the end of the for n_list is still empty.

First Post!

wmnnd

wmnnd

Remember that in Elixir, all variables are immutable. So what you’’e doing there simply has no effect, neither on list nor on n_list. At the end of this code, list and n_list still point to the same values as they did before.

Last Post!

NobbZ

NobbZ

You can get that result with my function shown in Appending elements to a list - #6 by NobbZ

Your function does use imperative assignement and will produce warnings in Elixir 1.4 and newer (perhaps earlier, I’m not sure exactly when this has been deprecated) and will not work anymore in 2.0.

A version that works better were this:

def foo(list) when is_list(list), do: foo(list, Enum.count(list), [])

defp foo(list, 0, acc), do: acc
defp foo(list, n, acc) when n > 0, do: foo(list, n - 1, [List.delete_at(list, n - 1) | acc])

It has the same problems according runtime as my already shown version using for.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement