3

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 
J Fritsch
  • 3,338
  • 1
  • 18
  • 40
  • 1
    Are you really asking for mutability, or do you just want simpler way of creating a new version of some immutable data? In the former case, rethink that. Especially if you come from a language that allows mutability. –  Dec 29 '11 at 13:00
  • I want to find a simple/the way w the least cost "of creating a new version of some immutable data". – J Fritsch Dec 29 '11 at 13:07
  • 1
    Define "cost". You aren't worrying about performance, are you? If so, shame on you. Get back to learning ;) –  Dec 29 '11 at 13:10
  • check http://stackoverflow.com/questions/413930/haskell-how-to-compose-not-with-a-function-of-arbitrary-arity – sdcvvc Dec 29 '11 at 13:12
  • 3
    You are probably going about this the wrong way, there are mutable datastructires in the io and st monad, but unless you need to, let ghc sort it out for you and use purly functional datastructures. – HaskellElephant Dec 29 '11 at 13:16
  • 1
    @delnan I don't think comments like "shame on you" are supportive to anything or anyone. – J Fritsch Dec 29 '11 at 13:28
  • 2
    I was tounge-in-cheek, as the smiley indicates (or so I hoped). But seriously, you seem to be worrying about non-issues. Besides the usual "microoptimization is the root of all evil" stuff, GHC-compiled Haskell is generally very fast even with all-immutable data. Just write idiomatic Haskell code - and that involves a great deal of immutability, with the options for mutability being introduced much later and used very carefully. –  Dec 29 '11 at 13:33
  • Thanks. This and the answer concerning the st monad solved my question. – J Fritsch Dec 29 '11 at 13:43

3 Answers3

3

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))
Sarah
  • 6,565
  • 1
  • 33
  • 44
2

As you say, Haskell uses immutable data types. There are STRefs and IORefs for doing mutable-like things, but if you are learning Haskell then you want to get the hang of doing things immutably first.

Haskell does have record accessors, but not for tuples. If you were to write something like this:

data MyRecord {
   ipAddress :: String,
   magicNumber :: Integer,
   messages :: [String]
} deriving Show

then you can use the field names as get functions on the fields. Given

test = MyRecord "192.168.1.1" 12345 ["aaaa", "bbbb", "cccc"]

then you can get the messages using the expression "messges test", or in the IO monad:

print $ messages test

You can create a new version of "test" using some special syntactic sugar, like this:

test2 = test {magicNumber = 654321}

This sets test2 to be a copy of test, except with a different magicNumber field.

So you could say:

test3 = test {messages = "first" : messages test}

Which I agree is a bit clunky, but you don't actually need it very often.

There are some proposals floating around for an improved record system for Haskell in which the above syntactic sugar actually becomes a function, but there is no consensus behind any of them. You should probably write your own modifiers where necessary, such as

modifyMessages :: ([String] -> [String]) -> MyRecord -> MyRecord
modifyMessages =  -- Left as an exercise for the student

Then you can implement your example as

test4 = modifyMessges ("first" :) test
Paul Johnson
  • 17,438
  • 3
  • 42
  • 59
  • Thanks. In fact a Haskell code often looks so pretty that I could not believe that something as "clunky" as test1, test2, test3 etc. can really be true. I just hoped to find something very elegant that I have just not come across yet. – J Fritsch Dec 29 '11 at 21:00
1

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 fclabels package:

import Data.Label
import Control.Category
import Prelude hiding ((.))


test = ("192.168.1.1", 3455, (1234566, msgs))
msgs = ["aaa", "bbbb", "ccccc"]

append x = modify messages (x :)

main = do
     let test' = append "first" test
     print test'
     print (get messages test')

second2 = lens (\(a,b) -> b)   (\b (a,_) -> (a,b))
third3  = lens (\(a,b,c) -> c) (\c (a,b,_) -> (a,b,c))
messages = second2 . third3

The second2 and third3 can either be generic functions that you define once for all the tuples you need, or they can be something with meaningful names like messages, or timestamp. Anyways, this is something that is written once, and then hidden in the library.

Testing with GHCi gives the desired result:

ghci> :main

("192.168.1.1",3455,(1234566,["first","aaa","bbbb","ccccc"])) 
["first","aaa","bbbb","ccccc"]
aleator
  • 4,436
  • 20
  • 31