Haskell函数组合的一些错误和困难

MyB*_*Bug 0 haskell

f x = x + 3
g x = x * 3
Run Code Online (Sandbox Code Playgroud)
<interactive>:17:1: error:

    ? Non type-variable argument in the constraint: Num (a -> c)
      (Use FlexibleContexts to permit this)
    ? When checking the inferred type
        it :: forall c a. (Num (a -> c), Num c) => a -> c
Run Code Online (Sandbox Code Playgroud)

我的函数组合运算符出错了.为什么不起作用?f x工作,g x工作,甚至f(g x)工作,但f.g x不起作用.

lis*_*rus 6

代码f . g x不起作用,因为它被解析为f . (g x).也就是说,首先g应用于x,然后你尝试获得f结果的组成g x.

为了使其工作,您可以用括号括起合成(f . g) x,或使用$操作符,它具有所有操作符的最低优先级,因此可用于分隔事物:f . g $ x.