haskell编译解析错误(可能是不正确的缩进)



我是haskell的新手,我正在努力使以下代码发挥作用:

abc :: fooType
abc = do
let loop c d = do 
let q = borrow "a"
d2 = d + q
c2 = c + 1
if c2 == 10
then 
if d2 >= 60
then
maybeB <- cast "b"
return $ isJust maybeB 
else
return $ False
else
loop c2 d2

loop 0 0

我一直得到:

error: parse error on input `let'
let q = borrow "a"

代码在我看来是正确的,这会是缩进的问题吗?我知道do块有一些关于如何设置缩进的特定规则,但从我读到的内容来看,它看起来是正确的。。,有人能发现问题出在哪里吗?

ps:借还Int>0,cast可能返回int。但是在第2行上编译失败

letdo都是块纹章——这意味着下一个词法标记的开始启动一个块并设置该块的缩进级别。此外,缩进必须增加以嵌套块。因此:

abc = do -- block herald
-- "let" is the next token after a block herald, so it sets an indentation level;
--   it is also itself a block herald
-- "loop" is the next token after a block herald, so it sets an indentation level
-- "do" is a block herald
let loop = do
-- "let" is the next token after a block herald, so it sets an indentation level,
--   **and must be deeper than the enclosing indentation;**
--   it is also a block herald
-- "q" is the next token after a block herald, so it sets an indentation level
let q = ...
-- "d2" must use the indentation level of the block it wants to be in;
--   presumably the enclosing "let"
d2 = ...
c2 = ...
-- this "if" must use the indentation of the block it wants to be in;
--   presumably the closest enclosing "do" block
if ...
-- this "then" can be indented the same as the "if" above it, but I find
--   that confusing; my preferred style indicates the nesting structure
then ...
else ...
-- this "loop" must use the indentation of the block it wants to be in; presumably the outermost "do" block
loop

第二行有很多事情要做,所以很容易忘记你需要考虑的所有事情。不管怎样,代码中的主要错误是loop设置了缩进级别,并且在它之后不久的let必须比它缩进更多

最新更新