Ticket #2148: Bug.2.hs

File Bug.2.hs, 1.6 KB (added by twhitehead, 6 years ago)

Further (greatly) simplified code still exhibiting the problem.

Line 
1module Main where
2
3import System
4import qualified System.IO as IO
5
6import qualified Data.ByteString as BS
7import qualified Data.ByteString.Internal as BS (w2c,c2w)
8
9--------------------------------------------------------------------------------
10-- main loop
11--
12-- Setup the above process stream according to command-line paramaters, pass
13-- the contents of the file through it (a line at a time), and write each
14-- result (a collection of processed elements) out to seperate temporary file.
15---
16main = do IO.hSetBuffering IO.stdout IO.LineBuffering
17          args <- getArgs :: IO [String]
18          if length args /= 2 then do prog <- getProgName
19                                      fail $ "command line: " ++ prog ++ " size depth"
20                                      else return ()
21          size <- catch (read $ args !! 0) $ \_ -> fail $ "size should be an integer not " ++ (show $ args !! 1) :: IO Int
22          depth <- catch (read $ args !! 1) $ \_ -> fail $ "depth should be an integer not " ++ (show $ args !! 2) :: IO Int
23          forever 0 size depth
24    where read :: (Read a) => String -> IO a
25          read xs = case [ x | (x,xs') <- reads xs, ("","") <- lex xs' ] of
26                    [x] -> return x
27                    _   -> fail "read: ambiguous parse"
28          forever :: Int -> Int -> Int -> IO ()
29          forever i s d = do putStrLn $ "Starting block " ++ show i
30                             build d s line [] `seq` forever (i+1) s d
31          build :: Int -> Int -> BS.ByteString -> [BS.ByteString] -> [BS.ByteString]
32          build 0 _ _ xs                    = xs
33          build d s l xs | BS.length l >= s = x `seq` (build (d-1) s (BS.tail l) $ x : xs) where x = BS.take s l
34          build d s _ xs                    = build d s line xs
35          line :: BS.ByteString
36          line = BS.pack [BS.c2w '0' .. BS.c2w 'Z']