1
\$\begingroup\$

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)]
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.