Use an accumulator:

reversezipLists :: [a] -> [b] -> [(b, a)]
reversezipLists = go [] where
  go acc [] _ = acc
  go acc _ [] = acc
  go acc (h1:t1) (h2:t2) = go ((h2, h1) : acc) t1 t2

Bonus points: the function is tail recursive. The basic idea is that we append new pairs to the acc during the left-to-right traverse through the lists. The stackish nature of Haskell lists makes the result reversed. Note that it will truncate longer list.


If you want to throw an error when the lists’ lengths don’t match, you can play a bit with patterns:

reversezipLists :: [a] -> [b] -> [(b, a)]
reversezipLists = go [] where
  go acc [] [] = acc
  go acc (h1:t1) (h2:t2) = go ((h2, h1) : acc) t1 t2
  go _ _ _ = error "lists' sizes don't match" 

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top