懒惰地读取整数列表作为字节串

Read a list of integers lazily as a bytestring

我正在尝试查找文件中的整数之和。使用普通字符串的代码是:

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

我尝试将其更改为使用 Data.ByteString.Lazy 模块,如下所示:

import Data.ByteString.Lazy as L

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

但这被拒绝了,因为 words 正在返回一个字符串。然后我尝试使用 Data.ByteString.Char8 但它使用了严格的 ByteString。

如何让这个函数完全懒惰?

我找到了一个稍长的变通方法,可以将文件作为 ByteString 读取,然后作为整数列表读取。感谢@melpomene

import Data.ByteString.Lazy.Char8 as L
main = do
    contents <- L.getContents
    print (sumFile contents)
         where sumFile x = sum $ Prelude.map tups $ Prelude.map L.readInt (L.words x)
             where read' = tups.(L.readInt)


tups :: (Num a) => (Maybe (a, b)) -> a
tups (Just (a,b)) = a
tups Nothing = 0