要從GHCi中的列表中去掉最後一個項目,我可以反轉列表,取出尾部,然後再將其反向。例如,讓功能組合工作不同嗎?
reverse(tail(reverse([1,2,3,4])))
由於有相當多的支架存在的,我想我會改變其使用功能組成代替。但是,當我嘗試這個時,我得到以下錯誤。
Prelude> reverse . tail. reverse [1,2,3,4]
<interactive>:2:17:
Couldn't match expected type `a0 -> [a1]' with actual type `[a2]'
In the return type of a call of `reverse'
Probable cause: `reverse' is applied to too many arguments
In the second argument of `(.)', namely `reverse [1, 2, 3, 4]'
In the second argument of `(.)', namely
`tail . reverse [1, 2, 3, 4]'
我認爲,這意味着它不喜歡作曲reverse [1,2,3,4]
,所以我試圖把它周圍的括號,但它給了我同樣的錯誤。
Prelude> reverse . tail. (reverse [1,2,3,4])
<interactive>:3:18:
Couldn't match expected type `a0 -> [a1]' with actual type `[a2]'
In the return type of a call of `reverse'
Probable cause: `reverse' is applied to too many arguments
In the second argument of `(.)', namely `(reverse [1, 2, 3, 4])'
In the second argument of `(.)', namely
`tail . (reverse [1, 2, 3, 4])'
但是,如果我做了以下它能正常工作。
Prelude> let f = reverse . tail . reverse
Prelude> f [1,2,3,4]
[1,2,3]
這是什麼原因導致這個錯誤,爲什麼讓綁定停止發生?
只需提一下,init函數就可以完成這項工作。 – zurgl
@zurgl我知道,這只是我遇到的問題的一個例子;) –