0

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?

1
  • unpair returns a pair, not a list. Hence, both your examples are wrong. Use case .. of (x,y) -> or let (x,y)=... in to access the components of a pair. Commented Dec 2, 2014 at 18:51

1 Answer 1

4

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].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.