Search code examples
haskellghci

Haskell From First Principles Ch5, Ex 7,8


I'm new to Haskell and reading Haskell from first principles.

right now I'm on chapter 5. while solving its exercises, specifically 7,8 I could not understand why I'm not coming up with right answer

so here is the question

u can find questions and solutions here

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

  1. Integer
  2. Int
  3. a
  4. (Num a, Ord a) => a
  5. Ord a => a
  6. Num a => a

I think its answer is 5. Ord a => a, bcs its one of its possible implementation which I have come up with is to completely ignore parameter b

like this

kessel ::(Ord a, Num b) => a -> b -> a
kessel a b = a
--if u have any other implementation Please share

since Its completely ignoring b it should not effect the type of a but still ghci show its type is

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

What I'm missing ? same goes with Q:8


Solution

  • Num b is a red herring. The Num a constraint is because you passed a numeric literal for the a type, and those require the Num typeclass. Try :t kessel [] 2 to see it go away.