简体   繁体   English

为什么 function 结合需要 haskell 中的分析?

[英]why does function combine require parantesis in haskell?

Prelude> -- I have 2 functions: f and g
Prelude> f x y = x + y
Prelude> g x = 2*x
Prelude> f 2 3
5

To express $ f(x, g(y))* with x=2 and y=3 this work well:用 x=2 和 y=3 来表达$ f(x, g(y))*这很好用:

Prelude> f 2 (g 3)
8

why does the following return error?为什么以下返回错误?

Prelude>
Prelude> f 2 g 3

<interactive>:19:1: error:
    • Non type-variable argument in the constraint: Num (a -> a)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num (a -> a)) => a
Prelude> 
f 2 g 3

is (because function application left-associative ):是(因为 function 应用程序左关联):

f 2 g 3 = ((f 2) g) 3

that's why you get this error - it expects g there to be a Num (as it is the parameter y in fxy = x+y and +:: Num a -> a -> a -> a )这就是为什么你得到这个错误 - 它期望g有一个Num (因为它是fxy = x+y+:: Num a -> a -> a -> a中的参数y

2 as a literal can be a value in every Num a but GHC does not know a instance for Num that is a function a -> a . 2作为文字可以是每个Num a中的值,但 GHC 不知道Num的实例是 function a -> a

Now the error itself talks about the context - basic Haskell cannot have a constraints of the form Num ((->) aa) - but you could easily (and safely) circumvent this with the given extension... then you should get the error with the type-class.现在错误本身谈到了上下文 - 基本 Haskell 不能有Num ((->) aa)形式的约束 - 但您可以使用给定的扩展名轻松(并且安全地)规避它......然后你应该得到错误与类型类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM