Haskell Language Basic examples


Example

λ> :t 1
1 :: Num t => t

λ> :t pi
pi :: Floating a => a

In the examples above, the type-checker infers a type-class rather than a concrete type for the two constants. In Haskell, the Num class is the most general numerical one (since it encompasses integers and reals), but pi must belong to a more specialized class, since it has a nonzero fractional part.

list0 :: [Integer]
list0 = [1, 2, 3]

list1 :: [Double]
list1 = [1, 2, pi]

The concrete types above were inferred by GHC. More general types like list0 :: Num a => [a] would have worked, but would have also been harder to preserve (e.g. if one consed a Double onto a list of Nums), due to the caveats shown above.