type Data = (Int, Int, String) bottom (b, _, _) = b top (_, t, _) = t ide (_, _, i) = i contains :: Data -> Data -> Bool x `contains` y = bottom x <= bottom y && top x >= top y rangeSort1 :: [Data] -> [(Data, [Data])] rangeSort1 r = map (\x -> (x, (filter (contains x) (filter (/=x) r)))) r ---- test :: [Data] test = [ (11, 22, "A") , (22, 45, "B") , (22, 33, "C") , (25, 28, "D") , (47, 49, "E") ] #### sub bottom { shift->[0] }; sub top { shift->[1] }; sub ide { shift->[2] }; sub contains { bottom($_[0]) <= bottom($_[1]) && top($_[0]) >= top($_[1]) }; sub rangeSort1 { map { my $x = $_; [$x, [grep {contains($x, $_)} (grep { ide($_) ne ide($x) } @_) ] ] } @_ }; my $test = [ [11, 22, "A"] , [22, 45, "B"] , [22, 33, "C"] , [25, 28, "D"] , [47, 49, "E"] ]; #### rangeSort1 :: [Data] -> [(Data, [Data])] rangeSort1 r = mapMaybe (\x -> let l = filter (contains x) . filter (/=x) in case l r of [] -> Nothing otherwise -> Just (x, l r) ) r