Programming with primitive types
Haskell comes with several built-in primitive types that are used in most programs.
Int and Integer
We have already used the Int
type of integers in several examples. It supports four common arithmetic infix operators:
(+)
– addition(-)
– subtraction(*)
– multiplication(^)
– exponentiation
The (-)
operator can also be used as a prefix operator to negate a number. Besides these operators, two useful arithmetic functions are as follows:
div
– integer divisionmod
– modulo
A common beginner mistake is to use the (/)
operator for Int
, but it is only defined for floating-point types such as Float
and Double
.
The Int
type only covers a finite range of integers. The Haskell language specification guarantees that this covers at least the integers in the range from -229 to (229-1), but the actual range can be implementation dependent. For example, in GHC 8.10.2...