Haskell: What is the difference between `Num [a] => a` and` Num a => [a] `

My signature seems to be disabled. Since then I found out why. Now I'm interested in learning more about the alleged GHCI signature on my typo. I tried to get this code to work:

elemNum :: (Eq a, Num b) => a -> [a] -> b
elemNum e l = f e l
  where  f _ [] = []  -- this was my typo, supposed to read 0
         f e (x:xs)
             | x == e = 1 + f e xs
             | otherwise = f e xs

Obviously, this does not work for the reason indicated above; but if I delete my signature, it compiles (I don’t know why, please explain), and I get this signature:

elemNum :: (Num [a], Eq t) => t -> [t] -> [a]

I have never seen a typeclass Num [a]before. What does it mean and how does it compare with (Num a) => [a].

+3
source share
2 answers

Num a , a ; . a , a, a a. Integer Double .

, Num [a] , [a] . a, a. , ( ). , , , GHC , , , , , .

, :

foo (x:xs) = xs + 1

xs - , , , , , , .

+8

Num [a] , Num type.

foo :: Num a => [a]

- , Num type

bar :: Num [a] => [a]

- , Num. , Num , , FlexibleContexts

bar :: Num [a] => [a]
bar = 42

P.S. . elemNum e l = f e l ⇒ elemNum = f. ,

elemNum _ [] = 0
elemNum e (x:xs)
  | x == e = 1 + f e xs
  | otherwise = f e xs
+2

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


All Articles