为什么喂养init在Haskell中不起作用?

Tsh*_*nga 0 haskell list function-composition

所以我正在写一行来获取列表的倒数第二个元素.最初我的代码是

mySLast x = last.take ((length x) - 1) x
Run Code Online (Sandbox Code Playgroud)

哪个last功能一直有效.意识到我的take业务已经包含在Haskell中,init所以我改写为

mySLast = last.init 
Run Code Online (Sandbox Code Playgroud)

这仍然不起作用.我觉得这是令人费解,因为init::[a]->[a]last::[a]->a所以他们绝对应该在组合的同态Hask类别.

我试过问Haskell它认为类型是什么,它说

ghci> :t last.init
last.init :: [c] -> c
ghci> last.init [3,2,4,1]

<interactive>:45:6:
    Couldn't match expected type ‘a -> [c]’
                with actual type ‘[Integer]’
     Relevant bindings include
       it :: a -> c (bound at <interactive>:45:1)
    Possible cause: ‘init’ is applied to too many arguments
     In the second argument of ‘(.)’, namely ‘init [3, 2, 4, 1]’
     In the expression: last . init [3, 2, 4, 1]
Run Code Online (Sandbox Code Playgroud)

即使

ghci> init [3,2,4,1]
[3,2,4]
ghci> last [3,2,4]
4
Run Code Online (Sandbox Code Playgroud)

所以我一定是误解了在Haskell中编写函数的东西.任何见解将不胜感激!

Lee*_*Lee 10

函数应用程序绑定比(.)这更紧密

last.init [3,2,4,1]
Run Code Online (Sandbox Code Playgroud)

被解析为

last . (init [3,2,4,1])
Run Code Online (Sandbox Code Playgroud)

您可以使用

(last . init) [3,2,4,1]
Run Code Online (Sandbox Code Playgroud)

要么

last . init $ [3,2,4,1]
Run Code Online (Sandbox Code Playgroud)