Handling numerical data
Like all general-purpose programming languages, Haskell too has a few different number types. Unlike other languages, the number types in Haskell are organized into a hierarchy via type classes. This gives us two things:
Check sat compiletime we aren't doing anything insane with numbers
The ability to write polymorphic functions in the number type with enhanced type safety
An example of an insane thing would be dividing an integer by another integer, expecting an integer as a result. And because every integral type is an instance of the Integral
class, we can easily write a factorial
function that doesn't care what the underlying type is (as long as it represents an integer):
factorial :: Integral a => a -> a factorial n = product [1..n]
The following table lists basic numeric types in Haskell:
Type |
Size |
---|---|
|
Signed integers, machine-dependent |
|
Unsigned integers, machine-dependent |
|
Double-precision floating point, machine-dependent |
|
Single... |