How to define a Haskell function with the argument "newtype"?

I defined a new type called Poly. Poly is a polynomial list view (Num list), and I'm trying to define a chop function that removes the excess 0 from the end of Poly.

Chop takes one Poly as an argument, and then returns Poly. For some reason, I get the following error message:

Expected restriction, but "Poly a looks like" * In the type signature for "chop: chop :: Poly a => a → a

newtype Poly a = P [a]

chop :: Poly a => a -> a
chop l = if (last l) == 0 then chop (init l) else l
+4
source share
1 answer

Haskell => . Haskell, , .

Poly a - , , chop, , :

chop :: Poly a -> Poly a
chop (P l) = if (last l) == 0 then chop (P $ init l) else P l

, ! 0: (last l) == 0. , , Poly a , , , a Num a. , Poly (Maybe String). :

chop :: Num a => Poly a -> Poly a
chop (P l) = if (last l) == 0 then chop (P $ init l) else P l

: newtype, type, Haskell Poly a . , :

type Poly a = [a]

chop:

chop :: Num a => Poly a -> Poly a
chop l = if (last l) == 0 then chop (init l) else l
+10

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


All Articles