4
max' :: Int -> Int -> Int
max' a b = if a >= b then a else b 

you see that the function is correct but if i write

let a = 3,
let b = 3

and also if i write

ghci> a == b => True

so it compares them then why it doesn't compare in my function

ghci> max' a b 

error occurs why? or what is the right way to write it?

Sorry I am beginner if my question is silly forgive me for that and edit it if there is a need for that Thanks

<interactive>:19:6:
    Couldn't match expected type `Int' with actual type `Integer'
    In the first argument of max', namely `a'
    In the expression: max' a b
    In an equation for `it': it = max' a b

<interactive>:19:8:
    Couldn't match expected type `Int' with actual type `Integer'
    In the second argument of max', namely `b'
    In the expression: max' a b
    In an equation for `it': it = max' a b
Davorak
  • 7,362
  • 1
  • 38
  • 48
user2999428
  • 165
  • 1
  • 7
  • 2
    What error do you get? – Paul Manta Nov 17 '13 at 21:58
  • here it is :19:6: Couldn't match expected type `Int' with actual type `Integer' In the first argument of max', namely `a' In the expression: max' a b In an equation for `it': it = max' a b :19:8: Couldn't match expected type `Int' with actual type `Integer' In the second argument of max', namely `b' In the expression: max' a b In an equation for `it': it = max' a b – user2999428 Nov 17 '13 at 22:07

2 Answers2

9

I guess you are doing this in the ghci interpreter. Then, have a look at (:t displays the type of an expression and a line of the form a :: t means a has type t):

Prelude> let a = 3
Prelude> :t a
a :: Integer

The ghci interpreter commits early and gives a the type Integer though it should give any numeric type (thus a :: Num t => t).

Now, your function receives Ints as arguments but since a and b are Integers you get that error message.

You can either remove the restrictive type signature or you can define a and b to be Ints. I'd go with the first option, unless there is some requirement to go with Int-only type signature. To do so you need to add ::Int at the end of the definition:

Prelude> let b = 42 :: Int
Prelude> :t b
b :: Int

If you want to remove the signature recode your function to have only one line:

max' a b = if a >= b then a else b 

Now, if you're to inspect its type:

Prelude> :t max'
max' :: Ord a => a -> a -> a

Which means you've got a generic function which works for any type which can be ordered.

An alternative is to start ghci using an extension: ghci -XNoMonomorphismRestriction. In this case:

Prelude> let a = 3
Prelude> :t a
a :: Num a => a

which will work directly on your function.

The reason why ghci without this extension commits to Integer is the Monomorphism restriction

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
2

When you use let a = 3, the type of a will be Integer, not Int. You can check this by using :t a in ghci. You can use let a = 3 :: Int to ensure that you get the correct type:

ghci>let a = 3 :: Int
ghci>let b = 3 :: Int
ghci>max' a b 
Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
Zeta
  • 103,620
  • 13
  • 194
  • 236
  • Why it becomes integer not int directly? and what are the differences between prelude or ghci? – user2999428 Nov 17 '13 at 22:13
  • 2
    If you don't explicitly annotate, the compiler has to make a choice. Choosing `Integer` here is a choice as good as any. – gspr Nov 17 '13 at 22:30
  • 2
    GHCi is GHC's interactive interpreter. Prelude is just a standard module that's always implicitly imported. It is what gives you functions such as `>=` that you have in your code above. – gspr Nov 17 '13 at 22:33
  • Yes you are right Integer is a choice as good as any so why specifically this ? does it have any privilege ? – user2999428 Nov 17 '13 at 23:05
  • 3
    @gspr It does not *have to* choose. Since all integer literals have an implicit `fromInteger` to them `ghci` could keep it as a general `Num a => a` value. – kqr Nov 18 '13 at 09:04
  • 1
    @kqr: Ah, yes, I agree. That was imprecise of me. @user2999428: Well, all the other integral types (such as `Int`) have limited range. I'm only speculating now, but I guess it's kinda nice that the chosen type can handle any integer you enter (given enough memory)? – gspr Nov 18 '13 at 10:51