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 working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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[].