[Haskell-cafe] Re: Adding a field to a data record
Jon Fairbairn
jon.fairbairn at cl.cam.ac.uk
Wed Jul 29 05:06:27 EDT 2009
Henry Laxen <nadine.and.henry at pobox.com> writes:
> 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.
One approach to this would be to temporarily redefine Foo
data Foo = Foo { a :: Int, b :: Int,
... many other fields ...
y :: Int } deriving (Eq, Read, Show, Typeable, Data)
| NuFu {a :: Int, b :: Int,
... many other fields ...
y :: Int,
z :: Int} deriving (Eq, Read, Show, Typeable, Data)
read the file, map Foo to NuFoo + whatever the initial value of z is
and write it out again.
--
Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk
More information about the Haskell-Cafe
mailing list