storing to a file

Hal Daume III hdaume@ISI.EDU
Thu, 14 Nov 2002 08:02:41 -0800 (PST)


There's been mention of a Binary module; there is also one in the GHC CVS
repository under (I think) compiler/ghc/utils/Binary.hs.  There is
currently discussion on the libraries list about getting a Binary module
into the standard libraries.  We are currently working out some details,
but it will probably closely resemble the GHC Binary module (which is a
conversion of the NHC Binary module).

That said, there was also a post about using plain text.  I tend to agree,
except for certain cases.  However, that is *not* to say that you should
necessarily use Show/Read.  For instance, say you want to write something
of type [[Int]] to a file.  I would strongly discourage using show/read on
this, because read will need to read *all the way* to the end before
returning anything, to make sure there's that last close-bracket.  For
this case, I would much prefer to use:

writeInts fn = writeFile fn . unlines . map (unwords . map show)
readInts fn = readFile fn >>= return . (map (map read . words) . lines)

The same applies to tuples, etc.  This *vastly* inproves the efficiency
and, for long lists, tends to make them more human-readable (IMO).

 - Hal

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

On 14 Nov 2002, Johan Steunenberg wrote:

> Hello,
> 
> I am new to haskell, and to functional programming, and wondering how to
> store a Double, or any non-char, to a file. Do I have to make a char
> array of the double and store that? Or is it preferred to use the show
> and read functions? 
> 
> Thanks in advance,
> johan steunenberg
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>