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
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
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
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
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








