1

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!

MdxBhmt
  • 1,310
  • 8
  • 16

1 Answers1

3
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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • That did the trick! I was stuck because before inserting inverfalFinder, the code worked OK (I had map (Bl.pack . B.unpack) in place) and couldn't figure it out why oneworked and the other didn't. – MdxBhmt Aug 30 '12 at 20:50
  • 1
    The `hClose` code is unsafe (may leak the handle in case of exceptions). That's what `withFile` is for. – Peaker Aug 30 '12 at 20:56