小编use*_*083的帖子

Haskell懒惰的字节串字不懒惰?

我有以下Haskell程序用于计算整数字符串的最大和子串:

{-# LANGUAGE BangPatterns #-} {-# OPTIONS_GHC -O2 #-}
import Data.Functor
import Data.Maybe 
import Data.ByteString.Lazy.Char8 (getContents,lines,readInt,words)
import Prelude hiding (getContents,words,lines)

main = do
    cont <- words <$> getContents
    putStrLn $ show $ snd $ foldl opt (0,0) $ map (fst.fromJust.readInt) cont

opt (!c,!m) x = (max 0 (c+x),max m (c+x))
Run Code Online (Sandbox Code Playgroud)

该程序的问题在于它将整个文件读入内存.没有BytesString的相应程序没有这个问题:

{-# LANGUAGE BangPatterns #-} {-# OPTIONS_GHC -O2 #-}
import Data.Functor
import Data.Maybe 

main = do
    cont <- words <$> getContents
    putStrLn $ show $ snd $ foldl opt (0,0) …
Run Code Online (Sandbox Code Playgroud)

haskell long-lines bytestring lazy-io

3
推荐指数
1
解决办法
279
查看次数

标签 统计

bytestring ×1

haskell ×1

lazy-io ×1

long-lines ×1