Updating lists inside another list

I have a list of pairs. Each pair has a value and a list. So, the structure looks like this: [ (a, [b, c]), (b, [a, c]) ] What I want to do is be able to add an element to the inner list of the pair with the head value of my choosing. So, for the above list:
addToInnerList [ (a, [b, c]), (b, [a, c]) ] a d [ (a, [b, c, d]), (b, [a, c]) ]

On Tue, Jun 23, 2009 at 8:05 AM, Aaron MacDonald
I have a list of pairs. Each pair has a value and a list. So, the structure looks like this:
[ (a, [b, c]), (b, [a, c]) ]
What I want to do is be able to add an element to the inner list of the pair with the head value of my choosing. So, for the above list:
addToInnerList [ (a, [b, c]), (b, [a, c]) ] a d [ (a, [b, c, d]), (b, [a, c]) ]
A simple recursive definition will do this: addToInnerList [] _ _ = [] addToInnerList (x@(h,l):xs) a b | h == a = (h,l++[b]):xs | otherwise = x : addToInnerList xs a b BTW, is it your homework? lee
participants (2)
-
Aaron MacDonald
-
Lee Duhem