Haskell在最后调整以删除参数变量

use*_*988 3 haskell pointfree function-composition

我是一个尝试学习haskell的新手,我试图在其他论坛中寻找类似的东西但是找不到类似的问题.

addPoly :: (Num a)=>[[a]]->[a]
addPoly  x = map sum $ transpose x
Run Code Online (Sandbox Code Playgroud)

运行正常

但是当我最后删除x时,它会出错

addPoly :: (Num a)=>[[a]]->[a]
addPoly  = map sum $ transpose 
Run Code Online (Sandbox Code Playgroud)

错误说:

Couldn't match expected type `[[Integer]] -> [Integer]'
            with actual type `[Integer]'
In the expression: map sum $ transpose
In an equation for `addPoly': addPoly = map sum $ transpose

Couldn't match expected type `[[Integer]]'
            with actual type `[[a0]] -> [[a0]]'
In the second argument of `($)', namely `transpose'
In the expression: map sum $ transpose
In an equation for `addPoly': addPoly = map sum $ transpose
Run Code Online (Sandbox Code Playgroud)

无法弄清楚我在这里失踪了什么.

免责声明:这不是一个功课问题

Dog*_*ert 7

$ 在Haskell中定义为

f $ x = f x
infixr 0 $
Run Code Online (Sandbox Code Playgroud)

因此,如果您展开代码的第一个片段,

map sum $ transpose x
Run Code Online (Sandbox Code Playgroud)

map sum (transpose x)
Run Code Online (Sandbox Code Playgroud)

哪个会奏效.

但第二个片段

map sum $ transpose 
Run Code Online (Sandbox Code Playgroud)

map sum transpose
Run Code Online (Sandbox Code Playgroud)

当你打电话给x你时,你会得到

map sum transpose x
Run Code Online (Sandbox Code Playgroud)

它实际映射sumtranspose(并用参数调用结果x,这也没有意义,并导致你得到的错误信息,因为map它将返回一个List,而不是一个函数),而不是结束transpose x.

您需要使用此.功能,而不是$定义为

(.) f g = \x -> f (g x)
Run Code Online (Sandbox Code Playgroud)

如果你这样做,你的代码

map sum . transpose
Run Code Online (Sandbox Code Playgroud)

\x -> map sum (transpose x)
Run Code Online (Sandbox Code Playgroud)

当你从一些参数调用x它时,它就变成了

map sum (transpose x)
Run Code Online (Sandbox Code Playgroud)

这是我们开始的(正确)代码.

如果有什么不清楚,请告诉我.