Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update list enclosed in tuple

Tags:

haskell

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 
like image 715
J Fritsch Avatar asked May 07 '26 13:05

J Fritsch


1 Answers

As others have stated, there should be very specific and weighty reasons for not doing things the functional way. Your tuple seems needlessly convoluted, but you could go about creating a new one with the extra message like this:

addMsg (a,b,(c,msgs)) msg = (a,b,(c,msg:msgs))
like image 136
Sarah Avatar answered May 11 '26 15:05

Sarah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!