Basically I want to achieve a tuple (or any datatype that fits my problem) that contains a list that can be updated. Because of immutability and such the below demonstrator code does not work of course. It is only to illustrate better what I want to achieve.
I wonder of there are any elegant ways, datatypes (IOREfs?) or modules that would do that for me or if in such a case I really would need to take apart the whole tuple, and reconstruct it with the updated elements.
By the way: intuitively I would have expected that at least as long as test is not printed the list msgs will not be evaluated.
test = ("192.168.1.1", 3455, (1234566, msgs))
msgs = ["aaa", "bbbb", "ccccc"]
main = do
--print test
let msg_tmp = "first" : msgs
let msgs = msg_tmp
print msgs
print test
Have you considered using lenses? If your purpose is to make updating the tuple easy and transparent for the programmer, they fit the bill very nicely. The following uses the excellent
fclabelspackage:The
second2andthird3can either be generic functions that youdefine once for all the tuples you need, or they can be something
with meaningful names like
messages, ortimestamp. Anyways, thisis something that is written once, and then hidden in the library.
Testing with GHCi gives the desired result: