Constants in Haskell
Constants in Haskell
Haskell supports constants of character, string, boolean, and numeric values.
Defining Constants
In Haskell, constants can be defined using the let
or where
clauses, but for simplicity, constants are usually defined at the top level using pattern matching.
Defining Numeric Constants
A constant statement can appear anywhere a variable assignment can.
Arithmetic with Constants
Constant expressions in Haskell perform arithmetic with arbitrary precision.
Type Conversion
A numeric constant has no type until it’s given one, such as by an explicit conversion.
Context-Based Typing
A number can be given a type by using it in a context that requires one, such as a variable assignment or function call. For example, here sin
expects a Floating
type.
To run the program, save the code in a file named constants.hs
and use runhaskell
to execute it.
Now that we can run and build basic Haskell programs, let’s learn more about the language.