Why is there no zip index with respect to the count argument?
Haskell newbie here. I think that:
-
zip
andzip3
are important functions - included in the prelude, implemented in many other languages, are a common operation in mathematics (transposition) -
not generic to the parameter structure
-
easy to implement in traditional languages - C or C ++ (say 20 hours of work); python already has it as built-in
Why is it zip
so limited? Is there an abstraction generalizing it? Is there something wrong with n-dimensional tuples?
source to share
Since the suggested duplicates answer most of these questions, I will focus on the questions in your follow-up comment.
1) why the standard implementation for fixed n = 2
zipWith
for 2 arguments, and repeat
for 0 arguments. This is enough to get arbitrary zips. For example, version 1 of the argument (also called map
) could be implemented as
map f = zipWith ($) (repeat f)
and 3 versions of the argument as
zipWith3 f = (.) (zipWith ($)) . zipWith f
etc. There is quite a pattern for implementations of large zips (admittedly not obvious from this small sample size). This result is similar to the one in CT, which says that any category with 0-ary and 2-ary products has all finite products.
The other half of the answer, I suppose, is that level type numbers (which are the most common injection method for arbitrary zip files) are possible, but annoying and avoided, tend to reduce both terms and level noise.
2) I need to pass the number of lists which are cumbersome
Use ZipList
. You don't need to pass in the number of lists (although you do need to write one infix operator in a list - a very light requirement I think, since even in Python you need a comma between each list).
Empirically: I have not found arbitrary zips such a common need that I would call it "cumbersome".
3) even if I define my own zip code there will be collisions with Prelude.zip.
So choose a different name ...?
source to share
Since the type signatures will be different, for example, the tick signatures of zip and zip3 are different:
zip :: [a] -> [b] ->[(a,b)]
zip3:: [a] -> [b] -> [c] -> [(a,b,c)]
zip3
takes one more argument than zip
, and secondly, a type, and haskell doesn't allow you to have polymorphism with different number of arguments due to currying. It explains here that currying is on SO.
source to share