Haskell,语法错误

时间:2013-01-08 03:30:19

标签: parsing haskell syntax

temperatura :: Float->Float
temperatura qCalor
    | qCalor == 0 = 10
    | 0 < qCalor < 3 = 30--fTem1
    | 3 <= qCalor <= 9 = 50
    | qCalor > 9 = 60--fTemp2
    | 15 <= qCalor <= 24 = 150
    | 24 < qCalor <= 27 = 170--fTemp3
    | otherwise = "Nao existe temperatura correspondente a esse calor no grafico!"

优先级解析错误 那是为什么?

2 个答案:

答案 0 :(得分:7)

首先,在提出问题时,发布实际错误会很有帮助。这是我得到的:

test.hs:3:7:
    Precedence parsing error
        cannot mix `<' [infix 4] and `<' [infix 4] in the same infix expression

test.hs:4:7:
    Precedence parsing error
        cannot mix `<=' [infix 4] and `<=' [infix 4] in the same infix expression

test.hs:6:7:
    Precedence parsing error
        cannot mix `<=' [infix 4] and `<=' [infix 4] in the same infix expression


test.hs:7:7:
    Precedence parsing error
        cannot mix `<' [infix 4] and `<=' [infix 4] in the same infix expression

现在,问题基本上是<(和其他比较运算符)实际上只是二进制函数 - 带有两个参数的函数。编译器告诉您它无法知道如何在表达式中放置括号,因为这些函数具有相同的优先级。举个例子,这个:

| 0 < qCalor < 3 = 30

编译器不知道它是(0 < qCalor) < 3还是0 < (qCalor < 3)。在任何情况下,该行都没有合理的输入。

我建议像(0 < qCalor) && (qCalor < 3)这样的东西,或者更好的是,使用像(这可能是内置的)这样的函数:

betweenNums a b c = (a < b) && (b < c)

答案 1 :(得分:2)

请注意python样式的表达式,如:

x < y < z

不合法的Haskell。 即使这样也不对:

(x < y) < z

由于:

Prelude> :t (<)
(<) :: Ord a => a -> a -> Bool

与&lt;,&gt;,&lt; =和&gt; =进行比较的内容必须是同一类型。 (x < y)会产生一个Bool。下一步(在您的情况下)将是Bool < Float,这是不可能的。