Haskell returns type confusion

When working through “Haskell Programming from First Principles,” I am puzzled by the answer to the following question:

If the type kesselis equal (Ord a, Num b) => a -> b -> a, then the type kessel 1 2is

a) Integer
b) Int
c) a
d) (Num a, Ord a) => a
e) Ord a => a
f)Num a => a

Answer: d), but I think the answer should be e), since the only requirement in the type signature for the first argument (a) is that it is Ord. So why not come back?

kessel 'd' 2

valid and has type Char- nothing about Num!

What? I do not understand?

Thank you for your help.

+4
source share
2 answers

1 - Num a => a. kessel, (Num a, Ord a) => a, kessel Ord . , (Num a, Ord a) => a .

, . , . :

kessel :: Ord a => a -> a

kessel 1 (Num a, Ord a) => a. Num , 1 - kessel.

kessel 'd' 2 Char, Ord Char => Char, Char , Ord, .

+5

kessel 'd' 2

Char - Num!

! kessel 1 2 kessel 'd' 2 ,

λ> kessel = undefined :: (Ord a, Num b) => a -> b -> a
kessel :: (Num b, Ord a) => a -> b -> a

λ> :t kessel 1 2
kessel 1 2 :: (Num a, Ord a) => a

λ> :t kessel 'd' 2
kessel 'd' 2 :: Char

, kessel .

+1

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


All Articles