[Haskell-beginners] can somebody explain the type of this expression?

Ovidiu Deac ovidiudeac at gmail.com
Mon Oct 27 20:47:19 UTC 2014


Prelude> let f x = x * 2
Prelude> :t f
f :: Num a => a -> a

The typeclass Num is defined like this:

class  Num a  where
    (+), (-), (*)       :: a -> a -> a
    ...

...which means that the operator (*) expects two parameters of type a and
returns a value of type a.

Since the definition of expr looks like this:
Prelude> let f x = x * 2

...and 2 is an Int, I would expect that the type inferred for (*)  is (Int
-> Int -> Int) and thus f should be (Int -> Int)

Can somebody explain this?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20141027/49372532/attachment.html>


More information about the Beginners mailing list