John Kim
John Kim

Reputation: 1158

Haskell remove parenthesis for a tuple

I'm a beginner in Haskell, and this is a question for an assignment. I'm pretty much done the problem, but I can't figure out how to remove the parenthesis that is surrounding a tuple that I calculated.

get_balance_partition :: Int -> [([Int], [Int])] -> (Int, ([Int], [Int]))
get_balance_partition min (x:xs)
    | null xs && difference_partitions x == min = (min, x)
    | null xs = (min, ([], []))
    | difference_partitions x == min = (min, x)
    | otherwise = get_balance_partition min xs

This is a helper function for working code, and I use it like this :

get_balance_partition 2 (two_partitions [7, 4, 3, 6, 10])

output : (2,([7,4,3],[6,10]))

I want to get rid of the parenthesis surrounding the pair of partitions so that the result,

 (2,([7,4,3],[6,10]))

looks like

 (2,[7,4,3],[6,10])

How can I get rid of the parenthesis when the pair of partitions is stored in x?

Upvotes: 1

Views: 570

Answers (1)

luqui
luqui

Reputation: 60463

There are no standard functions that work on triples, you have to write them yourself.

munge :: (a,(b,c)) -> (a,b,c)
munge (x,(y,z)) = (x,y,z)

munge (get_balance_partition 2 (two_partitions [7, 4, 3, 6, 10]))

Or anonymously as a lambda

(\(x,(y,z)) -> (x,y,z)) (get_balance_partition 2 (two_partitions [7, 4, 3, 6, 10]))

Another way is to modify your definition to match on the tuple in the list directly:

get_balance_partition :: Int -> [([Int], [Int])] -> (Int, ([Int], [Int]))
get_balance_partition min ((x,y):xs)
   ...

And use (x,y) instead of x everywhere, then you can return e.g. (min, x, y) in the first case, and similarly in other cases.

Upvotes: 4

Related Questions