> While I'm finding the "Gentle Tutorial" and "The Report" to be fairly
> easy to follow in general, I am having trouble figuring out how to read
> binary files in and out of Haskell programs. I didn't see any relevant
> examples in the progs/demo directory. Could someone please send me some
> simple concrete examples?
The binary files in Haskell are (IMHO) not what they should ought to be.
They are only intended to communicate data between Haskell programs
using the same Haskell system. The format of the binary files are
completely unspecified.
> Specifically, there are two types of files I would like to read. The
> first kind of file is a speech waveform data file, which is stored as a
> binary file, with the samples stored as "signed short int". Can Haskell
> read a stream of "signed short int" or do I have to insert a translation
> program?
Strictly speaking you would have to insert a translation to text form.
BUT, I think all implementation so far (certainly hbc) will allow you
to open the file as an ordinary text file, you can then do the conversion
from the string to a list of integers.
Something like this (works with hbc):
convert :: String -> [Int]
convert (c1:c2:cs) =
let x = ord c1 * 256 + ord c2
in (if x >= 32768 then x-65536 else x) : convert cs
convert [] = []
convert _ = error "odd number of bytes"
main = readFile "file-of-signed-shorts" abort (\s ->
appendChan stdout (show (convert s)++"\n") abort done)
-- Lennart