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)]
asked Feb 19, 2020 at 0:32
\$\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
answered Feb 20, 2020 at 1:11
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.