gpt4 book ai didi

haskell - 没有最后一个参数的函数组成

转载 作者:行者123 更新时间:2023-12-02 10:44:06 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com