
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

Johan Steunenberg
how to store a Double, or any non-char, to a file.
I can give you a general advice: store it in ASCII format via show, unless you have *VERY* strong reasons against it. Yes, it results in bigger files (but you can compress them), and slower (what compression makes even worse) but you gain easy view/edit and compatibility. I couple of years ago I was very fond of binary formats... Feri.

Hello Feri, thanks for your advice, I guess it sweetens the situation, though I really would like to know how to store in a binary format. Have a nice day, Johan Am Don, 2002-11-14 um 11.15 schrieb Ferenc Wagner:
Johan Steunenberg
writes: how to store a Double, or any non-char, to a file.
I can give you a general advice: store it in ASCII format via show, unless you have *VERY* strong reasons against it. Yes, it results in bigger files (but you can compress them), and slower (what compression makes even worse) but you gain easy view/edit and compatibility. I couple of years ago I was very fond of binary formats...
Feri. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 14 Nov 2002, Johan Steunenberg wrote:
thanks for your advice, I guess it sweetens the situation, though I really would like to know how to store in a binary format.
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne/haskell_libs/Bin... might be interesting for you. Actually, "deriving binary" would be a nice thing to have in general - even more, a way to add your own "deriving" things from within Haskell, although I have no idea how such a thing would work. http://www.pms.informatik.uni-muenchen.de/forschung/haskell-wish-list/items.php3?sort=p&mono=y seems to be a bit broken at the moment so I don't know if that relates to any proposed extensions. Are there any pages that summarise what people have learned from trying out already-implemented extensions, to help get an idea what the next Haskell will be like, or what ideas to try next? For instance, I expect to see some concurrency and exceptions, multi-parameter type classes, etc. make it through. -- Mark

For some reason, the York ftp links never work for me, so here are the links for the Binary data papers (practically all of their webpages only point to "ftp://" for papers): Heap Compression and Binary I/O in Haskell Malcolm Wallace and Colin Runciman http://www.cs.york.ac.uk/ftpdir/pub/malcolm/hw97.html The Bits Between The Lambdas: Binary Data in a Lazy Functional Language Malcolm Wallace and Colin Runciman http://www.cs.york.ac.uk/ftpdir/pub/malcolm/ismm98.html Chris Milton --- Mark Carroll wrote:
On 14 Nov 2002, Johan Steunenberg wrote:
thanks for your advice, I guess it sweetens the situation, though I really would like to know how to store in a binary format.
http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne/haskell_libs/Bin...
might be interesting for you. Actually, "deriving binary" would be a nice thing to have in general - even more, a way to add your own "deriving" things from within Haskell, although I have no idea how such a thing would work.
http://www.pms.informatik.uni-muenchen.de/forschung/haskell-wish-list/items.php3?sort=p&mono=y
seems to be a bit broken at the moment so I don't know if that relates to any proposed extensions. Are there any pages that summarise what people have learned from trying out already-implemented extensions, to help get an idea what the next Haskell will be like, or what ideas to try next? For instance, I expect to see some concurrency and exceptions, multi-parameter type classes, etc. make it through.
-- Mark
__________________________________________________ Do you Yahoo!? Yahoo! Web Hosting - Let the expert host your site http://webhosting.yahoo.com

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

G'day all. On Thu, Nov 14, 2002 at 09:56:24AM -0500, Mark Carroll wrote:
Actually, "deriving binary" would be a nice thing to have in general - even more, a way to add your own "deriving" things from within Haskell, although I have no idea how such a thing would work.
Actually, there's one situation where it would work easily, and that is when used with newtype. Apart from "Show" and "Read", which are special cases anyway, the "deriving" operation on newtype has a simple meaning: inherit instances from the type being wrapped. Thoughts? Cheers, Andrew Bromage

Actually, there's one situation where it would work easily, and that is when used with newtype. Apart from "Show" and "Read", which are special cases anyway, the "deriving" operation on newtype has a simple meaning: inherit instances from the type being wrapped.
GHC does this on newtypes already.

Johan Steunenberg wrote:
thanks for your advice, I guess it sweetens the situation, though I really would like to know how to store in a binary format.
Here's one possibility, using the Storable class: import Word import IOExts import Foreign.Ptr import Foreign.Storable import Foreign.Marshal.Alloc toOctets :: Storable a => a -> [Word8] toOctets x = unsafePerformIO $ do ptr <- malloc :: Storable a => IO (Ptr a) poke ptr value let bptr = castPtr ptr :: Ptr Word8 bytes <- mapM (peekElemOff bptr) [0 .. sizeOf x - 1] :: IO [Word8] free ptr return bytes However, this will use the internal format, which will vary between architectures, so a file written on one architecture may not work on a different architecture.

On Thu, Nov 14, 2002 at 09:56:24AM -0500, Mark Carroll wrote:
Actually, "deriving binary" would be a nice thing to have in general - even more, a way to add your own "deriving" things from within Haskell, although I have no idea how such a thing would work.
well, here's one way it might work: http://research.microsoft.com/~simonpj/Papers/derive.htm although i'm not exactly sure what you mean by 'add your own "deriving" things'... :) matt -- matt hellige matt@immute.net http://matt.immute.net

On Thu, 14 Nov 2002, matt hellige wrote: (snip)
well, here's one way it might work: http://research.microsoft.com/~simonpj/Papers/derive.htm
I'll take a look at that - thanks - it might answer a few of my "generic programming" questions.
although i'm not exactly sure what you mean by 'add your own "deriving" things'... :)
I was thinking that it might be nice to be able to write Haskell to add, say, a "deriving XML" or "deriving ASN1" feature whose instances provide methods to convert between Haskell data structures and those formats, instead of having to hack the compiler to achieve such automated method writing. BTW, those typed returns on sockets that Shawn mentioned sounded interesting. IIRC Modula-3 also had some approach to worrying about data exchange between older and newer versions of the same program. -- Mark

Thanks a lot. after being offline for three days, it is great to see the amount of answers. I'll have a try. And I realize even more that the world of Haskell is pretty different from my C++/Java world. (Though it reminds me of template meta programming) Have a nice week, Johan Steunenberg Am Fre, 2002-11-15 um 01.46 schrieb Glynn Clements:
Johan Steunenberg wrote:
thanks for your advice, I guess it sweetens the situation, though I really would like to know how to store in a binary format.
Here's one possibility, using the Storable class:
import Word import IOExts import Foreign.Ptr import Foreign.Storable import Foreign.Marshal.Alloc
toOctets :: Storable a => a -> [Word8] toOctets x = unsafePerformIO $ do ptr <- malloc :: Storable a => IO (Ptr a) poke ptr value let bptr = castPtr ptr :: Ptr Word8 bytes <- mapM (peekElemOff bptr) [0 .. sizeOf x - 1] :: IO [Word8] free ptr return bytes
However, this will use the internal format, which will vary between architectures, so a file written on one architecture may not work on a different architecture. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (8)
-
Andrew J Bromage
-
Christopher Milton
-
Ferenc Wagner
-
Glynn Clements
-
Hal Daume III
-
Johan Steunenberg
-
Mark Carroll
-
matt hellige