[Haskell-cafe] Re: Abstraction leak
Andrew Coppin
andrewcoppin at btinternet.com
Sun Jul 1 13:07:13 EDT 2007
Jon Fairbairn wrote:
> Andrew Coppin <andrewcoppin at btinternet.com> writes:
>
>
>> While we're on the subject... am I the first person to
>> notice that Haskell doesn't appear to have much support for
>> fiddling with streams of bits?
>>
>
> No. Presumably the author of Data.Bits noticed some
> lack. (Note that Integer is an instance of Num and hence
> Bits)
>
Right. But (for instance) there is no library function anywhere to
convert a Word16 into a [Bool] (never mind handling endian issues), or
back again. There is no standard construct for reading from a [Word8] as
if it's a [Bool], or for writing to a [Bool] and ending up with a [Word8]...
...Then again, there is no library function for reading from a file and
getting a [Word8]. The only way I have found to do this is to open a
file, put the handle into "binary mode", use hGetContents to read a
[Char] from it, and then do a map (fromIntegral . fromEnum) on that.
Writing binary data back to a file requires opening a handle, putting it
into binary mode, taking your [Word8] and doing a map (toEnum .
fromIntegral) over it, and then using putStr. All of which works
because, apparently, in binary mode the file is interpreted as 8-bit
ASCII. God forbit that Haskell ever decides to transparently support
other encodings...
I haven't actually tried, but presumably a TCP connection is represented
in the same way as a file, and so has the same problems.
Basically doing binary I/O seems to be one of those things that in
Haskell falls into the class of "it's possibly but annoyingly messy"...
More information about the Haskell-Cafe
mailing list