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

Maurí­cio CA mauricio.antunes at gmail.com
Tue Jul 28 10:55:39 EDT 2009

> 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.

This seems to depend on what you want to accomplish. Is
your goal just to rewrite this whole file? If it is, the
idea of just adding a field to Foo would be enough. You
could then add that 'z' field in your file using 'sed' (or,
as you said, emacs) and then read it back.

In general, however, if you want to deal with this kind
of translation of text to data, what you really want is to
take some time to learn something like Parsec.


> 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}

Note that this would not work exactly like that. 'a' is
a "field" of Foo, and that means it's a function like

a :: Foo -> Int

So, you can't use it as a field of Foo1, as that would

a :: Foo1 -> Int


More information about the Haskell-Cafe mailing list