error: Not in scope: type variable [Haskell]

Aaron Swartz

i want a list of numerical values in my data type

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

but i'm still getting this Error

error: Not in scope: type variable ‘a’
Code-Apprentice

According to the Haskell wiki

In Haskell 98, only functions can have type constraints.

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)

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related