Haskell -Main.HS:18:5:输入上的解析错误‘ putstrln’

发布于 2025-01-25 06:53:31 字数 890 浏览 1 评论 0原文

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

如果我发表评论或删除有问题的行(第18行),则该代码可以正常工作,但是我真的很想保留它,也可以理解我在做错了什么。尝试了许多排列并搜索堆后,我仍然无法破解它。任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里°也失望 2025-02-01 06:53:31

您可以在中定义showproblem 子句,因此:

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文