William Jones
William Jones

Reputation: 175

Haskell turning list of tuples into list of lists

I am trying to turn a list of tuples into a list of lists. For example, if I have the list [(9,1), (6,3), (4,1)], then this will turn into [[9, 6, 4],[6],[6]]. What's happening is that in the list of tuples [(a,b)], a represents a number from 0-9 and b represents the occurrence of that number, a will always be unique.

What I am trying to do it go go over the list n times, where n = maximum b in the list of tuples. Each time I go over the list, I take a and put it into a list, then decrement b by 1. If b == 0 then I just skip it.

So from my example, I take [9,6,4] and throw them into a list, then decrement the b of each of them, so now the list would look like [(9,0),(6,2),(4,0)]. Then going over again, I take [6], list of tuples now looks like [(9,0), (6,1), (4,0)]. Finally, take [6] one last time, and now ever b in the list of tuples is 0 so it is done.

I have created a a function that takes the 1st element from the list of tuples iff b is >= 1, but I don't know how I can iterate this over the updated list with all `b - 1' for each tuple.

turnIntList :: [(Integer, Integer)] -> [[Integer]]
turnIntList [] = []
turnIntList x = ([map (\(a, b) -> case (a,b) of  _ | b >= 1 -> a | otherwise -> -1) x])

I have also tried creating another helper function that takes a list of tuples and will turn them into a list depending on how large b is. From the main function, I would try to send [(a, 1), (b, 1)...] to create the list, then keep track of decrementing b here until it's done. So, for this function:

pairingL :: [(Integer, Integer)] -> [Integer] -> [Integer]
pairingL ((a,b):xs) l -- = if b /= 0 then [a,b-1] else []
    | null xs = if b == 1 then [a] ++ l else if b > 1 then [a] ++ l ++ pairingL [(a,b-1)] l else l
    | otherwise = 
        if b /= 0 then [a] ++ l ++ pairingL ((a,b-1):xs) l else pairingL xs l


pairingL [(9,1), (7,2), (5,1)]
[9,7,7,5]

pairingL [(1,1), (2,1), (3,1)]
[1,2,3]

pairingL [(1,2), (2,2), (3,2)]
[1,1,2,2,3,3]

I've tried looking into unzipping the list and working with that, iteration, and repeat but I can't figure out how to get the function to go over the list multiple times and updating the list with the new b values then going again.

In conclusion, what I am trying to do is something like:

    turnIntList [(9,3),(5,1),(2,1)]
    [[9,5,2],[9],[9]]

    turnIntList [(1,1),(2,1),(3,1),(4,1)]
    [[1,2,3,4]]

    turnIntList [(1,2),(2,2),(3,2)]
    [[1,2,3],[1,2,3]]

    turnIntList [(4,2),(6,1)]
    [[4,6],[4]]

Process: I am taking the first element from the tuples, adding them to a list, then subtracting the second element by 1. After doing this for each tuple in the list, I repeat the process until all the second elements for each tuple is 0

Important notes: again, in the list of tuples [(a,b)], a will ALWAYS be UNIQUE number from 0-9, and b >= 0

Upvotes: 0

Views: 538

Answers (1)

Elmex80s
Elmex80s

Reputation: 3504

This maybe

turnIntList :: [(Integer, Integer)] -> [[Integer]]
turnIntList [] = [] -- if it doesn’t compile use [[]]
turnIntList ls = [i | (i, _) <- ls] : turnIntList [(i, n - 1) | (i, n) <- ls, n - 1 > 0]

Upvotes: 1

Related Questions