Search code examples
haskellbytestring

Lazy ByteString strange behaviour or bug?


When I'm testing my function intervalFinder in GHCI it seems to be working, but when I try to compile it, I have NO output:

The function works on the input:

*Main> intervalFinder $[B.pack"first",B.pack"second",B.empty,B.pack"third",B.emp
ty]
Loading package bytestring-0.9.2.1 ... linking ... done.
["Start Time: first","End   Time: second","Start Time: third","End   Time: third
"]

And running main:

*Main> main
Loading package bytestring-0.9.2.1 ... linking ... done.
*Main> :q
Leaving GHCi.

prints in results.txt:

Start Time: firstEnd   Time: secondStart Time: thirdEnd   Time: third 

but if I run ghc test3.hs,the output file is 0kb (and obviously no data in it!)

Am I doing something wrong?

Code:

{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as Bl
import System.IO 
import System.Environment


intervalFinder :: [B.ByteString]->[B.ByteString]
intervalFinder x = helper x ""
    where
    helper (x:xs) "" 
        | x /= ""   = ((B.append (B.pack("Start Time: ")) x)):(helper xs x)
        | otherwise = helper xs ""
    helper (x:xs) y
        | x == ""   = ( (B.append (B.pack("End   Time: ")) y)):(helper xs "")
        | otherwise = helper xs x
    helper _ _      = []

main = do
    filehandle <- openFile "result.txt" WriteMode
    Bl.hPutStr (filehandle) .  Bl.fromChunks . intervalFinder $[B.pack"first",B.pack"second",B.empty,B.pack"third",B.empty]

Thanks!


Solution

  • main = do
        filehandle <- openFile "result.txt" WriteMode
        Bl.hPutStr (filehandle) .  Bl.fromChunks . intervalFinder
              $[B.pack"first",B.pack"second",B.empty,B.pack"third",B.empty]
    

    The output is buffered, so the programme exits without flushing the buffer with runghc or with a compiled binary. In ghci, all buffers are flushed when ghci is exited¹.

    When you openFile a file handle, you should hClose it after use. That also flushes the output buffer if it's opened in write or append mode.

    main = do
        filehandle <- openFile "result.txt" WriteMode
        Bl.hPutStr (filehandle) .  Bl.fromChunks . intervalFinder
              $[B.pack"first",B.pack"second",B.empty,B.pack"third",B.empty]
        hClose filehandle
    

    Alternatively, you can use writeFile

    main = Bl.writeFile "result.txt" $ Bl.fromChunks ...
    

    ¹ I'm not 100% sure of that, but experience supports it.