3

So, I'm just starting to teach myself Haskell out of the book Real World Haskell, and in the course of doing one of the exercises, I wrote the following code:

step acc ch | isDigit ch = if res < acc   
                              then error "asInt_fold: \
                                         \result overflowed"
                              else res
                      where res = 10 * acc + (digitToInt ch)
            | otherwise  = error ("asInt_fold: \
                                  \not a digit " ++ (show ch))

When I loaded it into GHCi 6.6, I got the following error:

IntParse.hs:12:12: parse error on input `|'
Failed, modules loaded: none.

I'm virtually certain that the error is due to the interaction of the "where" clause and the subsequent guard; commenting out the guard eliminates it, as does replacing the "where" clause with an equivalent "let" clause. I'm also pretty sure that I must have mangled the indentation somehow, but I can't sort out how.

Thanks in advance for any tips.

2 Answers 2

11

where can't be placed between guards. From paragraph 4.4.3.1 Function bindings in Haskell Report.

Sign up to request clarification or add additional context in comments.

Comments

9

Try:

step acc ch
    | isDigit ch = if res < acc then error "asInt_fold: result overflowed" else res
    | otherwise  = error ("asInt_fold: not a digit " ++ (show ch))
    where res = 10 * acc + (digitToInt ch)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.