2

This is the file I am trying to load:

import Data.List (foldl')
import Text.Printf (printf)
import Data.Char (ord)

--data IntParsedStr = Int | ParsingError
--data ParsingError = ParsingError String

asInt :: String -> Either String Integer
asInt "" = Left "Empty string"
asInt xs@(x:xt) | x == '-' = either Left (Right . ((-1) *)) (asInt' xt)
                | otherwise = either Left Right (asInt' xs)
                where asInt' :: String -> Either String Integer
                      asInt' "" = Left "No digits"
                      asInt' xs = foldl' step (Right 0) xs
                                where step :: Either String Integer -> Char -> Either String Integer
                                      step (Left e) _ = Left e
                                      step (Right ac) c = either Left (Right . (10 * ac + ) . fromIntegral) (charAsInt c)

charAsInt :: Char -> Either String Int
charAsInt c | between (ord '0') (ord c) (ord '9') = Right $ ord c - ord '0'
            | otherwise = Left $ printf "'%c' is not a digit" c

checkPrecision str = either error ((str == ). show) (asInt str)

between :: Ord t => t -> t -> t -> Bool
between a b c = a <= b && b <= c

It loads without any problem in ghci but in hugs I get this error:

ERROR "folds.hs":17 - Syntax error in expression (unexpected `)')

Line 17 is the last in the definition of asInt function

Edit:

Hi! I recently founded that this is in fact a known hugs issue as said in this question where there is a link to the Hugs 98 Users Guide where says that

Legal expressions like (a+b+) and (a*b+) are rejected.

4

1 回答 1

3

我相信这是 Hugs 中的一个错误,而不是 GHC 的自由。Haskell 98 报告(适用于 Hugs 用法)说

语法优先规则适用于以下部分。(op e)当且仅当以与;(x op e)相同的方式解析时是合法的 (x op (e))同样对于(e op). 例如,(*a+b)在语法上无效,但(+a*b)(*(a+b))是有效的。因为 (+) 是左结合的,(a+b+)在语法上是正确的,但(+a+b)不是;后者在法律上可以写成(+(a+b)).

我将其解释为允许,(10 * ac + )因为(*)(+)都是左关联的,并且确实(*)具有更高的优先级。

正如评论中所指出的,((10 * ac) + )两者都接受,因此也是一种解决方法。

有趣的是,这并没有在Hugs vs Haskell 98 页面中列出,所以可能 Mark P. Jones 对报告的这一部分的解读与我不同。我当然可以原谅他。Gofer 早在 Haskell 允许它们之前就实现了构造函数类,并且 Hugs 在编译时仍然比 GHCi 快,并且仍然提供更好的错误消息。

于 2014-02-02T17:51:48.453 回答