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
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.
you can not implement unzip
in a single map
\(a,b)->([a],[b]) :: (a,b) -> ([a],[b])
so
map \(a,b)->([a],[b]) :: [(a,b)] -> [([a],[b])]
instead you need two maps
unzip ls = (map ???,map ???)
fill in the blanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With