我正在尝试通过代码的出现来学习 haskell(我知道,现在已经很晚了),但我遇到了我无法理解的缩进问题,尽管我检查了haskell.org或wikibooks上我认为相关的部分。
具体来说,在缩进我的内部辅助函数时,如果我缩进 4 个或更少的空格(不计算主块的 4 个空格缩进),haskell 将发送解析错误,但如果缩进至少 5 个额外的空格,它将起作用。所以我需要将 let ... 至少缩进 9 个空格。但是,仅将主块缩进 4 个空格而不是 5 个空格似乎可以正常工作,我不明白其中的区别。
因此这是可行的:
solvePart1 :: String -> IO ()
solvePart1 input = do
let parsedInput = map (map read . words) . lines $ input :: [[Int]]
let validateReport report =
let differences = zipWith (-) report (tail report)
in all (\d -> d >= 1 && d <= 3) differences || all (\d -> d <= -1 && d >= -3) differences
let areReportsValid = map validateReport parsedInput
let validReportsCount = sum $ map fromEnum areReportsValid
putStrLn $ "Day 2, Part 1: " ++ show validReportsCount
但事实并非如此:
solvePart1 :: String -> IO ()
solvePart1 input = do
let parsedInput = map (map read . words) . lines $ input :: [[Int]]
let validateReport report =
let differences = zipWith (-) report (tail report)
in all (\d -> d >= 1 && d <= 3) differences || all (\d -> d <= -1 && d >= -3) differences
let areReportsValid = map validateReport parsedInput
let validReportsCount = sum $ map fromEnum areReportsValid
putStrLn $ "Day 2, Part 1: " ++ show validReportsCount
该问题与 do 与 let ... in 语法无关,因为将内部函数重写为 do 块不会改变该问题:仍然需要 5 个空格。
(我使用 vscode 作为编辑器,并使用堆栈进行构建,以防万一)