chan11347
so i look up the zip(list_of_lists) in github and it have some example
Zips corresponding elements from each list in list_of_lists.
The zipping finishes as soon as any list terminates.
Examples
iex> List.zip([[1, 2], [3, 4], [5, 6]])
[{1, 3, 5}, {2, 4, 6}]
iex> List.zip([[1, 2], [3], [5, 6]])
[{1, 3, 5}]
i want to make the def function like above
def zip([]), do: []
def zip(list_of_lists) when is_list(list_of_lists) do
do_zip(list_of_lists, [])
end
i want to understand what is list of lists and how to represent it when every list is not the same, thanks
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lucaong
If I understand, you want to reimplement
List.zip\1, is that correct? That’s a nice exercise.The
list_of_listsargument is, as the name says, a list of several lists, possibly of different lengths. Thezipfunction iterates through all the lists, grouping the nth element from each list into a tuple. It stops as soon as the shortest list is exhausted.What exactly you want to understand better? Is something in particular confusing you?
chan11347
i am trying to put the code into the exs and run it with erlang
and the return is said is a undefined function, so i want know is that anything just wrong since i cannot compilation with these code
lucaong
Well, there are a number of things that are wrong. First of all, the function must be defined within a module. Second, the recursive call to
crossuses the wrong number of arguments (crossis defined with one argument, but is called with two arguments incross(list_of_lists, [])).Also, this recursive call would never complete, because the
list_of_listis passed unchanged, and therefore will never get to the base case of empty list (unless it’s empty to start with).Usually, when implementing recursion, you start with a base case, and you make sure that the recursive call moves in the direction of the base case (if the base case is an empty list, the list should be consumed at each recursive call, so that it will eventually be empty).
For example, let’s define a function that takes a list of numbers and returns the sum:
Now, you can call the defined function:
chan11347
so it should be like this?
NobbZ
No.
The
{h1, h2}line does not have any effect, its an unused value.The
cross([t1], [t2])line will eventually just loop forever ascross([[]], [[]]). You do not need to wrap the tails in a list, as they already are lists.Also your base case will just return
0, which would make your function return0regardless of the input, if there wasn’t the infinite loop in the other case. As you want to construct a list in the function, you need to return a list. What do you think how should the return look like?In the other case, you need to construct the return value from the tuple you already build and forget, and the result of the recursive call. Something like
[the_tuple | result_of_the_recursive_call].lucaong
Well, you are making progresses, but there are still things that won’t really work in this code.
First of all, the first definition of
cross, for empty lists, returns0. I don’t know what you are trying to do (are you implementingzip?), but that seems at odd with the rest of the program. What is your expected output? If you are using recursion, your base clause should return that.Second, the tuple
{h1, h2}is created but never used nor returned, so right now it is useless. If you are implementingzip, you’ll want to accumulate these tuples to finally return them in a list.Remember that Elixir functions always return the result of their last expression.
Also,
t1andt2are already lists. If you wrap them in brackets you will get nested lists, which is probably not what you want.Here’s a nudge in the right direction:
When implementing it, pay attention to the order of the accumulated list, and also beware that this implementation would only work if the list arguments have the same length. You can add base cases to account for different lengths (I leave this to you).
In general, my recommendation is that you take your time to learn the basics of Elixir before you jump into recursion exercises: it will make it easier to understand why something is not working as it should.
chan11347
this is what i try to do about it
chan11347
update:
lud
This returns
nil, so your final result will be[{'a', 1},{'b', 2}, {'c', 3} | nil]which is called an improper list because it contains a non-list element as the final element (well it is more complicated but I can’t explain it properly).Note that
[{'a', 1}, {'b', 2}, {'c', 3} | nil]and
[{'a', 1}, {'b', 2}, {'c', 3}, nil]are different.
The first one is equivalent to
[{'a', 1} | [{'b', 2} | [{'c', 3} | nil]]]The second one to
[{'a', 1} | [{'b', 2} | [{'c', 3} | [nil | []]]]]lucaong
You are very close. As @lud explained, you still have an issue with the base case.
In order to solve it for good, think about your base case: if you would call
Ans0.cross([], []), what would you expect as a result? Right now, you getnil, but is this what you really should expect? Remember that in Elixirnilis different from[].