zip and zipWith in Idris 2Both zip and zipWith combine two sequences element-by-element. In Idris 2, these operations exist both for standard dynamic-length lists (List a) and fixed-length dependent vectors (Vect n a).
List a)For standard lists, zip and zipWith behave similarly to other functional languages like Haskell. If the input lists have unequal lengths, the resulting list truncates to the length of the shorter input list.
zipzip takes two lists and pairs up their corresponding elements into a list of tuples.
import Data.List
-- Pairing equal-length lists
sampleZipList : List (Nat, String)
sampleZipList = zip [1, 2, 3] ["apple", "banana", "cherry"]
-- Result: [(1, "apple"), (2, "banana"), (3, "cherry")]
-- Truncating mismatched lengths
sampleZipTruncated : List (Nat, String)
sampleZipTruncated = zip [1, 2, 3, 4, 5] ["a", "b"]
-- Result: [(1, "a"), (2, "b")]
zipWithzipWith takes a combining function along with two lists. It applies the function to each pair of corresponding elements.
import Data.List
-- Pairwise addition
sampleZipWithList : List Nat
sampleZipWithList = zipWith (+) [1, 2, 3] [10, 20, 30]
-- Result: [11, 22, 33]
-- Combining different types using a custom function
formatItem : Nat -> String -> String
formatItem index name = show index ++ ". " ++ name
sampleFormatted : List String
sampleFormatted = zipWith formatItem [1, 2] ["Alice", "Bob"]
-- Result: ["1. Alice", "2. Bob"]
Vect n a)When working with Data.Vect, the Idris 2 type checker enforces at compile-time that both vectors have the exact same length .
zip
import Data.Vect
v1 : Vect 3 Nat
v1 = [1, 2, 3]
v2 : Vect 3 String
v2 = ["x", "y", "z"]
zippedVect : Vect 3 (Nat, String)
zippedVect = zip v1 v2
-- Result: [(1, "x"), (2, "y"), (3, "z")]
zipWith
import Data.Vect
vAdd1 : Vect 3 Nat
vAdd1 = [1, 2, 3]
vAdd2 : Vect 3 Nat
vAdd2 = [10, 20, 30]
addedVect : Vect 3 Nat
addedVect = zipWith (+) vAdd1 vAdd2
-- Result: [11, 22, 33]
zipWithBecause zipWith operates element-wise, nesting zipWith allows us to easily execute multi-dimensional matrix operations.
import Data.Vect
-- Matrix type definition (m rows, n columns)
Matrix : Nat -> Nat -> Type -> Type
Matrix m n a = Vect m (Vect n a)
-- Matrix Addition using nested zipWith:
-- 1. Inner zipWith (+) adds elements of two 1D row vectors.
-- 2. Outer zipWith applies that row-addition across all m rows.
addMatrix : Num a => Matrix m n a -> Matrix m n a -> Matrix m n a
addMatrix = zipWith (zipWith (+))
| Context | Types | Length Mismatch Behavior |
|---|---|---|
List |
zip : List a -> List b -> List (a, b)zipWith : (a -> b -> c) -> List a -> List b -> List c |
Silently truncates to the shorter list length at runtime. |
Vect |
zip : Vect n a -> Vect n b -> Vect n (a, b)zipWith : (a -> b -> c) -> Vect n a -> Vect n b -> Vect n c |
Compile Error if lengths do not match. |