我的语法错误的来源

And*_*zie 1 haskell

我正在学习Haskell并且正在编写一本书中的示例程序.下面的这个模块将无法编译并在ghci和ghc中给出以下错误:

"Optimal.hs:15:23:解析错误输入'='"

任何想法,我都看不到它?


module Optimal (optimalPath) where

import RoadSystem

optimalPath :: RoadSystem -> Path
optimalPath roadSystem =
    let (bestAPath, bestBPath) = foldl roadStep ([], []) roadSystem
    in if sum (map snd bestAPath) <= sum (map snd bestBPath)
            then reverse bestAPath
            else reverse bestBPath

roadStep :: (Path, Path) -> Section -> (Path, Path)
roadStep (pathA, pathB) (Section a b c) =
    let timeA = sum (map snd pathA)
        timeB = sum (map snd pathB)
        forwardTimeToA = timeA + a
        crossTimeToA = timeB + b + c
        forwardTimeToB = timeB + b
        crossTimeToB = timeA + a + c
        newPathToA = if forwardTimeToA <= crossTimeToA
                        then (A, a):pathA
                        else (C, c):(B, b):pathB
        newPathToB = if forwardTimeToB <= crossTimeToB
                        then (B, b):pathB
                        else (C, c):(A, a):pathA
    in (newPathToA, newPathToB)
Run Code Online (Sandbox Code Playgroud)

Bak*_*riu 7

任何想法,我都看不到它?

当您使用制表符进行缩进时会发生这种情况.哈斯克尔标准任务承担的制表位8个字符,所以编译器实际上是认为你的代码是:

module Optimal (optimalPath) where

import RoadSystem

optimalPath :: RoadSystem -> Path
optimalPath roadSystem =
        let (bestAPath, bestBPath) = foldl roadStep ([], []) roadSystem
        in if sum (map snd bestAPath) <= sum (map snd bestBPath)
                then reverse bestAPath
                else reverse bestBPath

roadStep :: (Path, Path) -> Section -> (Path, Path)
roadStep (pathA, pathB) (Section a b c) =
        let timeA = sum (map snd pathA)
                timeB = sum (map snd pathB)
                forwardTimeToA = timeA + a
                crossTimeToA = timeB + b + c
                forwardTimeToB = timeB + b
                crossTimeToB = timeA + a + c
                newPathToA = if forwardTimeToA <= crossTimeToA
                                                then (A, a):pathA
                                                else (C, c):(B, b):pathB
                newPathToB = if forwardTimeToB <= crossTimeToB
                                                then (B, b):pathB
                                                else (C, c):(A, a):pathA
        in (newPathToA, newPathToB)
Run Code Online (Sandbox Code Playgroud)

肯定在第15行附近,你有23个角色:

        let timeA = sum (map snd pathA)
                timeB = sum (map snd pathB)
Run Code Online (Sandbox Code Playgroud)

这是一个语法错误.

要解决这个问题:

  • 不要使用制表符进行缩进. 每个编辑器都允许您配置Tab键以插入可配置数量的空格而不是一个选项卡,因此没有"我必须多次按空格键"问题.
  • 在编辑器中使用8的tabstop.请注意,如果您发送代码到别人,他修改代码时可能有同样的问题.