Haskell:为什么我可以在ghci中加载这个文件,但是当我在hugs中尝试加载时,会出现语法错误?
这是我正在尝试加载的文件:Haskell:为什么我可以在ghci中加载这个文件,但是当我在hugs中尝试加载时,会出现语法错误?,haskell,ghci,hugs,Haskell,Ghci,Hugs,这是我正在尝试加载的文件: 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"
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
导入数据列表(foldl')
导入Text.Printf(Printf)
导入数据字符(ord)
--数据IntParsedStr=Int | ParsingError
--数据ParsingError=ParsingError字符串
asInt::字符串->任意字符串整数
asInt”“=左“空字符串”
asInt xs@(x:xt)|x='-'=左(右)((-1)*)(asInt'xt)
|否则=左或右(asInt'xs)
其中asInt'::字符串->任一字符串整数
asInt'”=左“无数字”
asInt“xs=foldl”步骤(右0)xs
其中步骤::任意字符串整数->字符->任意字符串整数
步进(左e)u=左e
步进(右ac)c=左(右)(10*ac+)。自整数)(字符c)
charAsInt::Char->任意字符串Int
charAsInt c | between(ord“0”)(ord c)(ord“9”)=右$ord c-ord“0”
|否则=左$printf“'%c'不是数字”c
checkPrecision str=任一错误((str==).show)(asInt str)
介于:Ord t=>t->t->t->Bool之间
在a b c=a之间,我认为这是拥抱中的错误,而不是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)+
被双方接受,解决方法也是如此
有趣的是,这并没有列在报告中,所以马克·p·琼斯对报告这一部分的解读可能与我不同。我当然可以原谅他;Gofer早在Haskell允许的时候就实现了构造函数类,而Hugs在编译时仍然比GHCi快,并且仍然提供更好的错误消息。我猜是(10*ac+)
。。。当您执行((10*ac)+)时会发生什么情况?(我现在不能测试,所以我不想回答这个问题)它有效!谢谢然而,奇怪的是,一个口译员认为这是一个语法错误,而另一个DO.GHC在许多情况下被认为比标准任务更自由。