\$\begingroup\$
\$\endgroup\$
I am trying to implement a greedy zip method in Haskell where it does not drop the elements, instead, it will use some default value. I appreciate any feedback to make it more compact.
zip' [-1] [] r = r
zip' [] [-1] r = r
zip' [] b r = zip' [-1] b r
zip' a [] r = zip' a [-1] r
zip' a b r = (xa, xb) : (zip' xsa xsb r)
where
(xa: xsa) = a
(xb: xsb) = b
main = do
let l1 = [1, 2, 3]
let l2 = [3, 4]
print (zip' l1 l2 [])
-- expected: [(1,3),(2,4),(3,-1)]
1 Answer 1
\$\begingroup\$
\$\endgroup\$
r
is always []
. Simpler library functions can do some of the work.
zip' [] b = map (-1,) b
zip' a [] = map (,-1) a
zip' (x:xs) (y:ys) = (x, y) : zip' xs ys
answered Feb 20, 2020 at 1:11
lang-hs