Haskell“约束中的非类型变量参数" [英] Haskell "Non type-variable argument in the constraint"

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

问题描述

我已经在我的 REPL 中创建了一个部分应用函数的列表,如下所示:

I've created a list of partially applied functions in my REPL like so:

listOfPartiallyAppliedFunctions = map (*) [1..100]

然后我想创建完成函数应用程序的结果列表,我可以通过向 map 函数提供 lambda 来轻松完成,如下所示:

I would then like to create the list of results from completing the function application, which I can easily do by providing a lambda to the map function like so:

let results = map (\x -> x 4) listOfPartiallyAppliedFunctions

这基本上意味着将应用到 4 的函数 x 映射到部分应用函数列表上,其中 x 是列表中的每个部分应用函数.

Which basically means map the function x applied to 4 over the list of partially applied functions, where x is each partially applied function from the list.

然而,我认为接下来我可以写:

However, I thought it would then follow that I could write:

let results = map (4) listOfPartiallyAppliedFunctions

因为不需要为 map 函数提供 lambda,因为它应该知道将 4 应用于 listOfPartiallyAppliedFunctions 中包含的部分应用函数.

As there shouldn't be a need to provide a lambda to the map function as it should know to apply 4 to the partially applied functions contained in the listOfPartiallyAppliedFunctions.

但是,我收到此错误:

• Non type-variable argument in the constraint: Num ((a -> a) -> b)
  (Use FlexibleContexts to permit this)
• When checking the inferred type
    it :: forall a b. (Num a, Num ((a -> a) -> b), Enum a) => [b]

有人可以帮我解析这个错误吗?我以为 4 是类型构造函数 Num 的实例?

Can someone help me parse this error? I thought 4 was an instance of type constructor Num?

推荐答案

然而,我认为接下来我可以写:

However, I thought it would then follow that I could write:

let results = map (4) listOfPartiallyAppliedFunctions

不,如果你会执行 \x ->4 x,你可以用 4 替换它.但是由于 4 意味着它是一个 Num 实例,并且您可能没有创建函数 a ->b Num 的一个实例,编译器无法解决这个问题.因此,编译器说它没有找到将数字 4 转换为函数的方法,而且绝对不是将函数 Num a => 作为输入的函数.->a,然后将其转换为 b.

No, if you would have performed \x -> 4 x, you could replace it with 4. But since 4 means it is a Num instance, and you likely did not make a function a -> b an instance of Num, the compiler can not solve this. The compiler thus says that it does not find a way to convert the number 4 into a function, and definitely not a function that takes as input a function Num a => a -> a, and then converts this to a b.

然而,您可以将上述内容写成:

You can however write the above as just:

let results = map ($ 4) listOfPartiallyAppliedFunctions

因此,我们在这里对 中缀运算符 [Haskell-wiki] 进行分段="https://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html#v:-36-" rel="noreferrer">($) :: (a -> b) ->->b 函数.

Here we thus perform a sectioning of an infix operator [Haskell-wiki] on the ($) :: (a -> b) -> a -> b function.

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

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