要从 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]'
我认为这意味着它不喜欢 compose 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]
是什么导致了这个错误,为什么 let 绑定阻止了这个错误的发生?