[Haskell] Simple and Easy Persistence

Matthew M. Munz mmm92 at pantheon.yale.edu
Tue Dec 6 21:28:42 EST 2005


Hi all,

   I have some program data that I'd like to persist.  I could just  
use read and show and file I/O to store arrays as files. [1]  This  
works and it is easy and simple (which I like) but it is also  
inefficient and a little cumbersome.

   I imagine that this is a very common task, and I know of utilities  
in other environments like object databases, embedded databases, XML  
persistence, etc. that can make this kind of persistence simple *and*  
relatively efficient and reliable.  E.g.) I have found the HSQLDB  
embedded database [2] to be useful when writing Java applications.   
What do you do for quick and easy persistence in Haskell?  Are there  
any libraries or techniques that are especially useful for this?

[1]
 > type SimpleRecord = (Integer, Integer, Integer)
 > type SimpleIndex = Integer
 > type SimpleDB = (Array SimpleIndex SimpleRecord, FilePath)

 > loadDB :: FilePath -> IO SimpleDB
 > loadDB f = do h <- openFile f ReadMode
 >               s <- hGetContents h
 >               hClose h
 >               return $ r s
 >  where r :: String -> SimpleDB
 >        r = read

 > getRecord :: SimpleDB -> SimpleIndex -> SimpleRecord
 > getRecord (a, _) = (a !)

 > updateRecord :: SimpleDB -> SimpleIndex -> SimpleRecord -> SimpleDB
 > updateRecord (a, f) i r = (a//[(i,r)],f)

 > storeDB :: SimpleDB -> IO ()
 > storeDB sdb@(a,f) = do h <- openFile f WriteMode
 >                        hPutStr h (show sdb)
 >                        hClose h

 > sampleDB = (array (0,10) [(x,(x+1,x*2,42)) | x <- [0..10]],"output/ 
1.db.txt")

[2] http://hsqldb.org/

   - Matt Munz
     mmm92 at pantheon.yale.edu




More information about the Haskell mailing list