karambit
karambit

Reputation: 167

Processing lists

I am trying to figure out how to process a list like this:

[ [[4,6,7], [1,2,4,6]] , [[10,4,2,4], [1]] ]

This is a list of a list of list of integers

I want my function take take this list as input and return a list of integers that are in the list without duplicates. The result should be [4,6,7,1,2,10]. But I am having trouble in figuring out how to traverse it.

My first idea was to take a process each list of integer lists separately like: 1st = [ [4,6,7], [1,2,4,6] ] 2nd = [ [10,4,2,4] , [1] ] the main function will call another function to handle these recursively, but how will the function remember what was in the final list when it comes to handle the 2nd list? I dont think an accumulator will even work.

Upvotes: 2

Views: 86

Answers (1)

Shoe
Shoe

Reputation: 76240

The type of that list is (Num a) => [[[a]]] so you can just use concat to flatten the structure and then nub to remove duplicates:

nub . concat . concat $ [[[4,6,7], [1,2,4,6]], [[10,4,2,4], [1]]]
-- [4,6,7,1,2,10]

Live demo

Depending on how the data is actually structured and based on statistical considerations it might be more efficient to remove duplicates within internal lists before concatenating.

Upvotes: 8

Related Questions