5

诚然,我是 Haskell 新手。为了探索惰性,我在 ghci 中创建了一个函数,它返回它的第二个参数:

Prelude> let latter x y = y
latter :: t -> t1 -> t1

我可以使用 、 、 、 和 (以小数表示)类型的Char参数[Char]Num调用FloatingFractional

Prelude> latter 'x' 'y'
'y'
it :: Char

Prelude> latter "foo" "bar"
"bar"
it :: [Char]

Prelude> latter 1 2
2
it :: Num t1 => t1

Prelude> latter pi pi
3.141592653589793
it :: Floating t1 => t1

Prelude> latter 0.5 0.7
0.7
it :: Fractional t1 => t1

latter当我尝试将a 应用于以Fractional比率表示时,为什么会出现可怕的错误(以及这是什么意思) :

Prelude> 1/2
0.5
it :: Fractional a => a

Prelude> latter 1/2 1/2

<interactive>:62:1:
    Could not deduce (Num (a0 -> t1 -> t1))
      arising from the ambiguity check for ‘it’
    from the context (Num (a -> t1 -> t1),
                      Num a,
                      Fractional (t1 -> t1))
      bound by the inferred type for ‘it’:
                 (Num (a -> t1 -> t1), Num a, Fractional (t1 -> t1)) => t1 -> t1
      at <interactive>:62:1-14
    The type variable ‘a0’ is ambiguous
    When checking that ‘it’
      has the inferred type ‘forall t1 a.
                             (Num (a -> t1 -> t1), Num a, Fractional (t1 -> t1)) =>
                             t1 -> t1’
    Probable cause: the inferred type is ambiguous
4

1 回答 1

17

Haskell 中的函数应用程序绑定比其他任何东西都更紧密。所以

latter 1/2 1/2

读作

((latter 1) / (2 1)) / 2

应用21并不是一个热门的想法,因为latter它需要两个参数,latter 1实际上是一个函数。将功能除以某物也不是一个好主意。您可以使用一些括号来解决所有这些问题:

latter (1/2) (1/2)
于 2015-02-01T01:01:45.667 回答