func4
为什么不起作用?尝试加载时出现异常。
-- Works fine
func::Integer
func = sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]
-- Works fine
func2::Integer
func2 = sum . takeWhile (<10000) . filter odd $ map (^2) [1..]
-- Works fine
func3::Integer
func3 = _func [1..]
where _func = sum . takeWhile (<10000) . filter odd . map (^2)
--Doesn't work!
func4::Integer
func4 = _func (^2) [1..]
where _func = sum . takeWhile (<10000) . filter odd . map
异常消息:
src.hs:3:63:
Couldn't match type `[a] -> [b]' with `[c]'
Expected type: (a -> b) -> [c]
Actual type: (a -> b) -> [a] -> [b]
Relevant bindings include
_func :: (a -> b) -> c (bound at src.hs:3:15)
Probable cause: `map' is applied to too few arguments
In the second argument of `(.)', namely `map'
In the second argument of `(.)', namely `filter odd . map'
Failed, modules loaded: none.
最佳答案
_func (^2) [1..]
是相同的
(sum . takeWhile (<10000) . filter odd . map) (^2) [1..]
并不是
sum . takeWhile (<10000) . filter odd . map (^2) $ [1..]
因为某种明显的原因(对我而言)。
可能的解决方案之一是使
_func
采用映射函数:func4::Integer
func4 = _func (^2) [1..]
where _func f = sum . takeWhile (<10000) . filter odd . map f
关于haskell - 没有最后一个参数的函数组成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27521047/