posfr
posfr

Reputation: 11

Haskell -- Main.hs:18:5: parse error on input ‘putStrLn’

module Main where

reportResults :: [String] -> [Int] -> IO ()
reportResults fileNames exitCodes = do
    putStrLn "All Files"
    putStrLn "---------"
    putStr.unlines $ map ("    " ++) fileNames
    putStrLn ""

    let problems = filter (\p -> fst p /= 0) $ zip exitCodes fileNames

    putStrLn "Problematic Files"
    putStrLn "-----------------"
    mapM_ (putStrLn . showProblem) problems
                where showProblem :: (Int, String) -> String
                      showProblem (c, f) = "    " ++ show c ++ " - " ++ f

    putStrLn "Done!"  -- "Parse error on input...". OK if this line is removed.

main :: IO ()
main = do
    let fileNames = ["A", "B", "C", "D"]
    let exitCodes = [0,   1,   2,   3]
    reportResults fileNames exitCodes

The code works fine if I comment out, or remove, the offending line (line 18), but I would really like to retain it and also understand what I'm doing wrong. After trying many permutations and searching heaps, I still can't crack it. Any help would be greatly appreciated.

Upvotes: 1

Views: 164

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

You can define the showProblem function in a let clause, so:

reportResults :: [String] -> [Int] -> IO ()
reportResults fileNames exitCodes = do
    putStrLn "All Files"
    putStrLn "---------"
    putStr.unlines $ map ("    " ++) fileNames
    putStrLn ""

    let problems = filter (\p -> fst p /= 0) $ zip exitCodes fileNames

    putStrLn "Problematic Files"
    putStrLn "-----------------"
    let showProblem (c, f) = "    " ++ show c ++ " - " ++ f
    mapM_ (putStrLn . showProblem) problems
    putStrLn "Done!"  -- "Parse error on input...". OK if this line is removed.

Upvotes: 1

Related Questions