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 Answer 1

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.