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

Where Next?

Popular in Questions Top

New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
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
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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 47930 226
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement