I have the following list (it’s a length 2 list, but in my assignment I have a length +n list)
xxs = [(11,22,[(33,33,33),(44,44,44)]),(55,66,[(77,77,77),(88,88,88)])]
I’m trying to “replace” one 3-tuple (p1 or p2 or p3 or p4 from the image bellow) by list index (n) and by sub-list index (p).

The function, at the end, should be like:
fooo newtuple n p = (…)
For example: (replace p3 for (98,98,98):
fooo (98,98,98) 2 1
[(11, 22, [(33,33,33) , (44,44,44)]) , (55, 66, [(98,98,98),(88,88,88)])]
I planned the code like following this steps:
-
Access the pn that I want to change. I manage to achieve it by:
fob n p = ((aux2 xxs)!!n)!!p where aux2 [] = [] aux2 ((_,_,c):xs) = c:aux2 xs -
“replace” the 3-tuple. I really need some help here. I’m stuck. the best code (in my head it makes some sense) that I’ve done: (remember: please don’t be too bad on my code, I’ve only been studying Haskell only for 5 weeks)
foo n p newtuple = fooAux newtuple fob where fooAux _ [] = [] fooAux m ((_):ds) = m:ds fob n p = ((aux2 xxs)!!n)!!p where aux2 [] = [] aux2 ((_,_,c):xs) = c:aux2 xs -
Finally I will put all back together, using
splitAt.
Is my approach to the problem correct? I really would appreciate some help on step 2.
I’m a bit new to Haskell too, but lets see if we can’t come up with a decent way of doing this.
So, fundamentally what we’re trying to do is modify something in a list. Using functional programming I’d like to keep it a bit general, so lets make a function
update.That will now take an index, a function and a list and replace the
nthelement in the list with the result of the function being applied to it.In our bigger problem, however, we need to update in a nested context. Luckily our
updatefunction takes a function as an argument, so we can callupdatewithin that one, too!All
fooohas to do is call update twice (once within the other call) and do a little “housekeeping” work (putting the result in the tuple correctly). The(n-1)and(p-1)were because you seem to be indexing starting at1, whereas Haskell starts at0.Lets just see if that works with our test case: