Gupta

Gupta

Facing Problem while appending elements In a list

I was working on a Question which was ,

Input - A list of numbers - [1,2,3,4,5] #example

We have to produce the subsets of this list and display those subsets whose sum is equal to 10

Output Format - [ [1,2] , [2,3] , [4,5,6] ] #example

Means we have to append those subset in a list whose sum is 10 and make a nested list and print it as an output.

here is my code …

defmodule Combination do
  def combine([]) do
    []
  end
  def combine([head | tail]) do
    tail_combinations = combine(tail)
    merged_combinations = Enum.map(
      [[]] ++ tail_combinations,
      fn c -> c ++ [head] end
    )
    tail_combinations ++ merged_combinations
  end

#Main thing I am doing here, i am Checking the sum of the subsets but unable to append the list of subset into another list and print the final list

  def display(lis) do
    a=[]
    for vale <- Combination.combine(lis) do
      if Enum.sum(vale)==6 do
        a=a++[vale]
      end
    end
    IO.inspect(a)
  end
end
Combination.display([1,2,3,4])

Error :

erts@eyrc:~/fr_task0_9999/.vscode$ elixir practice.exs
warning: variable "a" is unused (if the variable is not meant to be used, prefix it with an underscore)
  practice.exs:17: Combination.display/1

[]
erts@eyrc:~/fr_task0_9999/.vscode$ 

Can anyone help me i am new to elixir and i got stuct at this point.
Tell me how can i append the list in another list and print the whole list at once .

thank you

Marked As Solved

tomkonidas

tomkonidas

variable are immutable, you would need to use something like Enum.reduce.

def display(list) do
  list
  |> Combination.combine()
  |> Enum.reduce([], fn val, acc ->
    if Enum.sum(val) == 6 do
      [val | acc]
    else
      acc
    end
  end)
end

To display the list you could just pipe the result to IO.inspect

Combination.display([1,2,3,4]) |> IO.inspect()

I think this should give you what you want but looking at your combine, you might neet to reverse order some things.

I get the following output with your combine and my display:

iex> Combination.display([1,2,3,4,5])
[[3, 2, 1], [5, 1], [4, 2]]

Also Liked

kokolegorille

kokolegorille

This is a typical code, just not for FP.
There is also a problem of scope, everything inside a do end block, will never make its way to the surrounding block.

tcoopman

tcoopman

The picture of the error up you posted literary says what’s wrong. There is no function defined in the module with that name that takes 1 argument. That is what the /1 means.
Without extra context (actual code) we cannot help you any further.

In the future please post the text of the errors and not a picture.

tomkonidas

tomkonidas

Maybe it would be easier if you could explain what your combine/1 is supposed to be doing. As in what do you expect the outcome to be when calling that function with a list of integers. We might be able to better help if we understand the reason for the function and not just the output

kokolegorille

kokolegorille

There are a significant amount of questions related to this…

You might show some of your code You have tried so far to increase the chance to have an answer. It is supposed to be a personal work.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement