[Haskell-cafe] newbie questions (read, etc., with Data.ByteString.Lazy.Char8)

Mike Coleman tutufan at gmail.com
Mon Oct 6 22:06:37 EDT 2008


Hi,

I could use a little help.  I was looking through the Real World
Haskell book and came across a trivial program for summing numbers in
a file.  They mentioned that that implementation was very slow, as
it's based on String's, so I thought I'd try my hand at converting it
to use lazy ByteString's.  I've made some progress, but now I'm a
little stuck because that module doesn't seem to have a 'read' method.

There's a readInt method, which I guess I could use, but it returns a
Maybe, and I don't see how I can easily strip that off.

So:

1.  Is there an easy way to strip off the Maybe that would allow an
equivalently concise definition for sumFile?  I can probably figure
out how to do it with pattern matching and a separate function--I'm
just wondering if there's a more concise way.

2.  Why doesn't ByteString implement 'read'?  Is it just that this
function (like 'input' in Python) isn't really very useful for real
programs?

3.  Why doesn't ByteString implement 'readDouble', etc.?  That is, why
are Int and Integer treated specially?  Do I not need readDouble?

Thanks,
Mike


-- lazy version (doesn't compile)

-- file: ch08/SumFile.hs

import qualified Data.ByteString.Lazy.Char8 as L

main = do
         contents <- L.getContents
         print (sumFile contents)
             where sumFile = sum . map read . L.words


More information about the Haskell-Cafe mailing list