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

标签 haskell types compiler-errors

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/

相关文章:

c++ - 如何制作一个返回 Ncurses 函数的函数

python - 尝试使用 Pyinstaller 从 .py 脚本创建 .exe 时出现运行时错误

types - ocaml 类型澄清(初学者)

haskell - Monad 还可以测量副作用

haskell - 我可以有一个名为 "/"的值构造函数吗?

haskell - 循环一元谓词

types - Coq:显示环境中一个或多个类型中的所有术语

android - 如何在Android Studio项目中添加Hamcrest库?

android - 编译器 Android Studio 中的错误?

haskell - 理解恒等仿函数