views:

173

answers:

2

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.

+7  A: 

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)
Josef
+6  A: 

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

Alexey Romanov