Week 9: Notes

characters
strings
tuples
let
where
ranges
infinite lists
standard type classes
numeric type classes
type conversions
list comprehensions

You can read about these subjects in Learn You a Haskell, chapters 1-4.

type defaulting

In Haskell, even numeric literals are polymorphic:

> :t 2
2 :: Num a => a
> :t 3.5
3.5 :: Fractional a => a

We see that 2 may have any type that's an instance of the Num type class (e.g. Int, Integer, Float, Double). Similarly, 3.5 may have any type that's an instance of Fractional (e.g. Float, Double). We may choose any compatible type that we like:

> 2 :: Integer
2
> 3.5 :: Float
3.5

Even when we combine literals using operators, the result may still be polymorphic:

> :t (2 + 2)
(2 + 2) :: Num a => a
> :t (2 + 3.5)
(2 + 3.5) :: Fractional a => a

However in some situations Haskell must choose a type. For example, suppose that we actually perform the addition:

> 2 + 2
4
> 2 + 3.5
5.5

What type did Haskell use in the calculation? Its rule is that if it must resolve an ambiguous numeric type, it will use the first of these types that is possible: Integer, Double. Thus, the first addition above was performed using values of type Integer, and the second used type Double.