Search code examples
haskelldictionaryunzip

How do I split a list of tuples into two lists in haskell using the map function?


This is for homework due yesterday but I do not want the answer just a point to the right direction please;)

I am trying to implement the unzip function using map and lambda with haskell.

:t unzip
[(a,b)] -> ([a],[b]) 

and so I am thinking that the lambda would look like \(a,b)->([a],[b]) and that sort of works except I am getting from my input of [(4,5),(7,5),(9,7)] => [([4],[5]),([7],[5]),([9],[7])] but I would have liked to have seen [4,7,9],[5,5,7]. So what am I doing wrong here?

Thanks in advance for pointing me in the right direction


Solution

  • Well, map :: (a -> b) -> ([a] -> [b]) returns a list, right? And you want your function to return two lists, so... you'll need to use map twice. Here's a skeleton for you to fill in:

    unzip xs = (map {- ??? -} xs, map {- ??? -} xs)
    

    Unfortunately, insisting on using map is inefficient, because it means you must make two passes over the list. You can do a bit better, but it's tricky! Give it a shot, then see how well you did by comparing it with GHC's implementation.