[Haskell-cafe] Adding a field to a data record

Henry Laxen nadine.and.henry at pobox.com
Tue Jul 28 10:29:13 EDT 2009

Dear Group,

It seems to me this should be easy, but I can't quite figure out
how to do it without a lot of typing.  Here is the question:

Suppose you have a data type like:
Data Foo = Foo { a :: Int, b :: Int, 
   ... many other fields ... 
 y :: Int } deriving (Eq, Read, Show, Typeable, Data)

Now I would like to add a field z :: Int to the end of Foo.  If
I have a ton of data out on disk, which I wrote with, say
writeFile "a.data" (show foo) -- where foo is a [Foo] say 1000
long, I would like to get a new "a.data" file which has a new
z::Int field.

So far the only way I can think of is to make a new Data Foo1,
which includes the z::Int, read in a.data as a list of Foo,
write a function like:

fooTofoo1 :: Foo -> Foo1
fooTofoo1 xx = Foo1 {a = a xx, ... y = y xx, z = 1}

then write the file back out, and perhaps use emacs to
query-replace all the Foo1's back to Foo's, add the z::Int field
back into Foo, and read it back.

Please tell me there is a better way.  Thanks in advance.
Best wishes, 
Henry Laxen

I have read syb1, and syb2 a couple of times now, but so far
haven't been able to connect it with this kind of problem.

More information about the Haskell-Cafe mailing list