i am new to haskell and i am trying to make the following code work:
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
i keep getting:
error: parse error on input `let'
let q = borrow "a"
the code looks correct to me, would that be a problem with indentation? I know do
blocks have some specific rules on how to set up indentation but from what i've been reading it looks correct.., can anyone spot what the problem is?
ps: borrow returns Int > 0, cast returns maybe int. But the compilation is failing on line2
CodePudding user response:
let
and do
are both block heralds -- that means that the start of the next lexical token starts a block and sets the indentation level of that block. Additionally, indentation must increase to nest blocks. So:
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
There's a lot going on in the second line, so it's easy to lose track of all the things you need to think about. Anyway, the main mistake in your code is that loop
sets an indentation level, and the let
shortly after it must be indented more than it.