Generic function composition in Haskell

穿精又带淫゛_ 提交于 2019-12-10 04:40:18

问题


I was reading here, and I noticed that, for example, if I have the following function definitions:

a :: Integer->Integer->Integer
b :: Integer->Bool

The following expression is invalid:

(b . a) 2 3

It's quite strange that the functions of the composition must have only one parameter.

Is this restriction because some problem in defining the most generic one in Haskell or have some other reason?

I'm new to Haskell, so I'm asking maybe useless questions.


回答1:


When you do a 2 3, you're not applying a to 2 arguments. You're actually applying a to it's only argument, resulting in a function, then take that function and apply it to 3. So you actually do 2 applications. So in a sense, what you have is not equivalent to this:

a :: (Integer, Integer) -> Integer
b :: Integer -> Integer
(b . a) (2, 3)

You could've done this, btw

(b . a 2) 3


来源:https://stackoverflow.com/questions/4551419/generic-function-composition-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!