Haskell - putStrLn多次调用不打印

时间:2016-09-09 14:36:46

标签: haskell printing io

嗨所以我有这个运行解析器的主要方法

main = do
  args <- getArgs
  let filename = head args
  contents <- readFile filename
  let c = parse parseProgram contents
  putStrLn "------------------------------------------------"
  putStrLn "THE PROGRAM WE HAVE PARSED"
  putStrLn "------------------------------------------------"
  putStrLn (show ((fst.head) c))
  return ()

当我运行这个程序时,对putStrLn的前三次调用没有打印到终端,它只显示已解析的程序。

任何帮助将不胜感激,如何打印所有打印电话?

1 个答案:

答案 0 :(得分:3)

这似乎不可能。我创建了一个更接近代码的最小示例。我使用Parsec因为我不确定你正在使用什么解析库。

parsec-trivial.hs的内容:

#!/usr/bin/env stack
{- stack
    --resolver lts-6.15
    --install-ghc
    runghc
    --package parsec
-}

import System.Environment
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Char

ws = many space

nat :: Parser Integer
nat = read <$> many digit

parseProgram = ws *> nat <* ws <* eof

main = do
  args <- getArgs
  let filename = head args
  contents <- readFile filename
  let c = parse parseProgram filename contents
  putStrLn "------------------------------------------------"
  putStrLn "THE PROGRAM WE HAVE PARSED"
  putStrLn "------------------------------------------------"
  putStrLn $ show c

foo.b的内容:

42

执行此程序是这样的:

$ ./parsec-trivial.hs foo.b
------------------------------------------------
THE PROGRAM WE HAVE PARSED
------------------------------------------------
Right 42