Haskell映射函数的约束错误中的非类型变量参数 [英] Non type-variable argument in the constraint error on Haskell map function

查看:76
本文介绍了Haskell映射函数的约束错误中的非类型变量参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个名为mapper2的函数,该函数将一个函数应用于两个列表:

I am writing a function called mapper2 that applies a function to two lists:

mapper2 :: (a-> b -> c) -> [a] -> [b] -> [c]
mapper2 f (x:xs) (y:ys) = (f x y) : (mapper2 f xs ys)
mapper2 _ _ _ = []

我能够编译该函数,但在应用该函数时会出错:

I am able to compile the function but get an error when I apply it:

*Main> mapper2 (\x -> x*2) [2,4] [4,6] 

<interactive>:4:1: error:
    • Non type-variable argument in the constraint: Num (b -> c)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall b c. (Num (b -> c), Num b) => [c]

有人可以向我解释如何解决此错误以及该错误意味着什么吗?

Can someone explain to me how to fix this and what the error means?

推荐答案

查看mapper2的类型.

mapper2 :: (a -> b -> c) -> [a] -> [b] -> [c]

现在查看您要传递的函数的类型.

Now look at the type of the function you're passing in.

(\x -> x * 2) :: Num a => a -> a

mapper2函数希望传入两个参数的函数,但是您的lambda只接受一个参数.

The mapper2 function expects a function of two arguments to be passed in, but your lambda only takes one argument.

这篇关于Haskell映射函数的约束错误中的非类型变量参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆