implementation of unzip function in haskell

haskell

Solution

You could do it inefficiently by traversing the list twice

myUnzip [] = ([], []) -- Defaults to a pair of empty lists, not null
myUnzip xs = (map fst xs, map snd xs)

But this isn't very ideal, since it's bound to be quite slow compared to only looping once. To get around this, we have to do it recursively

myUnzip [] = ([], [])
myUnzip ((a, b):xs) = (a : ???, b : ???)
    where ??? = myUnzip xs

I'll let you fill in the blanks, but it should be straightforward from here, just look at the type signature of `myUnzip` and figure out what you can possible put in place of the question marks at `where ??? = myUnzip xs`

Problem

I am trying to implement the unzip function, I did the following code but I get error. ``` myUnzip [] =() myUnzip ((a,b):xs) = a:fst (myUnzip xs) b:snd (myUnzip xs) ``` I know that problem is in the right side of the second line but I do know how to improve it . any hint please . the error that I am getting is ``` ex1.hs:190:22: Couldn't match expected type `()' with actual type `[a0]' In the expression: a : fst (myUnzip xs) b : snd (myUnzip xs) In an equation for `myUnzip': myUnzip ((a, b) : xs) = a : fst (myUnzip xs) b : snd (myUnzip xs) ex1.hs:190:29: Couldn't match expected type `(t0 -> a0, b0)' with actual type `()' In the return type of a call of `myUnzip' In the first argument of `fst', namely `(myUnzip xs)' In the first argument of `(:)', namely `fst (myUnzip xs) b' ex1.hs:190:49: Couldn't match expected type `(a1, [a0])' with actual type `()' In the return type of a call of `myUnzip' In the first argument of `snd', namely `(myUnzip xs)' In the second argument of `(:)', namely `snd (myUnzip xs)' ```

Original source