错误:不在范围内:类型变量[Haskell] [英] error: Not in scope: type variable [Haskell]

查看:98
本文介绍了错误:不在范围内:类型变量[Haskell]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个数据类型的数值列表

i want a list of numerical values in my data type

data Polynomial = Polynomial {xs ::(Num a) => [a] } deriving (Show)

但是我仍然收到此错误

error: Not in scope: type variable ‘a’

推荐答案

根据 Haskell Wiki

在Haskell 98中,只有函数可以具有类型约束.

In Haskell 98, only functions can have type constraints.

为了执行所需的操作,可以使用类型参数声明Polynomial.然后编写具有适当类型约束的函数.

In order to do what you want, you can declare Polynomial with a type parameter. Then you write functions with the appropriate type constraints.

data Polynomial a = Polynomial xs
    deriving (Show)

这允许您构造具体的多项式类型,例如Polynomial IntPolynomial Float甚至Polynomial String.在您的Polynomial类型上运行的函数可以声明对type参数的约束.例如,将两个多项式相加的函数可以具有以下签名:

This allows you to construct concrete polynomial types such as Polynomial Int or Polynomial Float or even Polynomial String. A function which operates on your Polynomial type can declare constraints on the type parameter. For example a function to add two polynomials can have the following signature:

(+) :: (Num a) => Polynomial a -> Polynomial a -> Polynomial a

冲洗并重复执行每种功能.

Rinse and repeat as appropriate for each function.

这篇关于错误:不在范围内:类型变量[Haskell]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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