Haskell type class instantiation -
i trying set (polya a) , (polyb a) instances of class polynomial, want implement coeffs, fromcoeffs, coeffsb, fromcoeffsb. not quite sure doing wrong, because receive error message saying functions not visible class polynomial. please?
class polynomial p --default implementations data polya = coeffs [a] deriving (show) data polyb = const | x (polyb a) deriving (show) --instances instance polynomial (polya a) coeffs (coeffs f)=f fromcoeffs f= coeffs f instance polynomial (polyb a) coeffsb (const f)= [f] coeffsb (x f a)= coeffsb f ++ [a] fromcoeffsb [] = error "wrong input!" fromcoeffsb [f]= const f fromcoeffsb lis@(_:t)= x (fromcoeffsb (init lis)) (last lis)
the following code compiles me:
class polynomial p coeffs :: p -> [a] fromcoeffs :: [a] -> p --default implementations data polya = coeffs [a] deriving (show) data polyb = const | x (polyb a) deriving (show) --instances instance polynomial polya coeffs (coeffs f)=f fromcoeffs f= coeffs f instance polynomial polyb coeffs (const f)= [f] coeffs (x f a)= coeffs f ++ [a] fromcoeffs [] = error "wrong input!" fromcoeffs [f]= const f fromcoeffs lis@(_:t)= x (fromcoeffs (init lis)) (last lis)
summary of changes:
- add methods
polynomial
class declaration. - remove type arguments in instance declarations.
- change
coeffsb
coeffs
,fromcoeffsb
fromcoeffs
everywhere. - outdent
polyb
instance declaration 1 space.
Comments
Post a Comment