我正在学习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)
任何想法,我都看不到它?
当您使用制表符进行缩进时会发生这种情况.该哈斯克尔标准任务承担的制表位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)
这是一个语法错误.
要解决这个问题: