如何理解Haskell的“1.2%3.4”的错误消息?如何理解Haskell的“1.2%3.4”的错误消息?
Prelude> :m +Data.Ratio
Prelude Data.Ratio> 4.3 % 1.2
<interactive>:11:1:
No instance for (Show a0) arising from a use of ‘print’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Show Double -- Defined in ‘GHC.Float’
instance Show Float -- Defined in ‘GHC.Float’
instance (Integral a, Show a) => Show (Ratio a)
-- Defined in ‘GHC.Real’
...plus 23 others
In a stmt of an interactive GHCi command: print it
Prelude Data.Ratio>
也许你应该直接设置表达式的类型? (4.3%1.2)::漂浮或水手。也许haskell无法为此表达式导出类型 – 2014-10-31 07:26:45
ghc给出的所有错误消息中,这是最糟糕的之一。它不会告诉你实际的问题。你的表达式的类型是'(积分a,分数a)=>比率a'。但是,没有一种既是“积分”也是“分数”的类型。默认失败(它应该真的告诉你),它会为表达式'print(4.3%1.2)'发出一个错误,而不是你实际输入的内容。你的问题是“比率”是两个*整数*的比率,所以'4.3%1.2'没有任何意义。 '4%3'或类似的工作正常。 – user2407038 2014-10-31 07:29:34