Reputation: 341
In haskell I'm trying to acheive to convert a list like [(1,2),(3,4),(5,6)] into two lists: [1,3,5] and [2,4,6] using a recursive function only
I tried this so far, but it doesn't work
unpair :: [(a,b)] -> ([a], [b])
unpair [] = []
unpair ((x,y):xs) = x : unpair(xs) y : unpair (xs)
however, when I only try to make a list of the first value in the tuples, it does work:
unpair [] = []
unpair ((x,y):xs) = x : unpair(xs)
Any idea what I'm doing wrong?
Upvotes: 0
Views: 317
Reputation: 5406
The type of unpair xs
is ([a], [b])
, so you can't append an element to it like to a list. But you can use let
statement to pattern-match the result from unpair xs
, then construct the tuple with the new head element added to each.
I won't quote the actual answer because this looks like home work.
The reason the second implementation works is because in that one the type of unpair
is [(a,b)]->[a]
.
Upvotes: 4