Haskell: parsing error with "where" and guard

So, I am just starting to teach myself Haskell from the book Real World Haskell, and in the course of performing 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 downloaded it in GHCi 6.6, I received the following error:

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

I am pretty sure that the error is due to the interaction of the where clause and the subsequent guard; commenting out the protection removes it, and replaces the where clause with the equivalent let clause. I am also sure that I somehow distorted the indentation, but I cannot figure out how to do this.

Thanks in advance for any advice.

+3
source share
2 answers

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)
+9
source

where . 4.4.3.1 Haskell.

+11

Source: https://habr.com/ru/post/1715771/


All Articles