Solving Equations in Haskell

I need to do an exercise, and I'm pretty lost ... I need to make an instance for the Horde with polynomials. This is my attempt:

data Pol = P [(Float,Int)] deriving Show instance Ord Pol where (Pol a) > (Pol b) = (maxGrado a) > (maxGrado b) || ((maxGrado a) == (maxGrado b) && (maxCoe a) > (maxCoe b)) (Pol a) < (Pol b) = (maxGrado a) < (maxGrado b) || ((maxGrado a) == (maxGrado b) && (maxCoe a) < (maxCoe b)) maxGrado :: [(Float,Int)] -> Int maxGrado [] = 0 maxGrado ((c,g):xs) = g maxCoe :: [(Float,Int)] -> Int maxCoe [] = 0 maxcoe ((c,g):xs) = c 

- error:

 ERROR file:.\Febrero 2011.hs:32 - Undefined data constructor "Pol" 

The mistake is very dumb, but it was an hour trying to solve it ... Can someone help me?

Thanks!

+4
source share
2 answers
 data Pol = P [(Int, Int)] 

In this declaration, Pol is the type constructor, and P is the only data constructor for this data type. In general, a data type can have several data constructors, so we have this difference.

A simple rule is that you should use a type constructor whenever you are talking about types, and a data constructor whenever you are talking about values.

In this case, you should use Pol in the head of the instance, but P in the templates for your functions.

 instance Ord Pol where (P a) > (P b) = ... (P a) < (P b) = ... 

Also note that type constructors and data constructors live in different namespaces and are never used in the same context. This means that they have the same name.

 data Pol = Pol [(Int, Int)] 

This will also work.

+9
source

I think you want to use P instead of Pol in your instance functions. Pol is a type, P is a constructor.

+3
source

Source: https://habr.com/ru/post/1369544/


All Articles